diff --git a/.gitignore b/.gitignore index fa04334..b46e1b5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ +dist/ *.sw? diff --git a/dist/api-log-appender.d.ts b/dist/api-log-appender.d.ts deleted file mode 100644 index 560ede7..0000000 --- a/dist/api-log-appender.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { LogMessage, LogLevel } from './log-message'; -import LogAppender from './log-appender'; -export declare class ApiLogAppender implements LogAppender { - readonly apiEndpoint: string; - authToken?: string | undefined; - batchSize: number; - minimumTimePassedInSec: number; - maximumTimePassedInSec: number; - threshold: LogLevel; - private http; - private msgBuffer; - private lastSent; - constructor(apiEndpoint: string, authToken?: string | undefined, threshold?: LogLevel); - appendMessage(msg: LogMessage): void; - private doPost; - private checkPost; -} -export default ApiLogAppender; diff --git a/dist/api-log-appender.js b/dist/api-log-appender.js deleted file mode 100644 index 9ab7115..0000000 --- a/dist/api-log-appender.js +++ /dev/null @@ -1,60 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.ApiLogAppender = void 0; -const axios_1 = require("axios"); -const log_message_1 = require("./log-message"); -class ApiLogAppender { - constructor(apiEndpoint, authToken, threshold) { - this.apiEndpoint = apiEndpoint; - this.authToken = authToken; - this.batchSize = 10; - this.minimumTimePassedInSec = 60; - this.maximumTimePassedInSec = 120; - this.threshold = log_message_1.LogLevel.ALL; - this.http = axios_1.default.create(); - this.msgBuffer = []; - this.lastSent = 0; - this.checkPost = () => { - const now = Date.now(); - const min = this.lastSent + this.minimumTimePassedInSec * 1000; - const max = this.lastSent + this.maximumTimePassedInSec * 1000; - if ((this.msgBuffer.length >= this.batchSize && min < now) || - (this.msgBuffer.length > 0 && max < now)) { - this.doPost(); - } - setTimeout(this.checkPost, Math.max(10000, this.minimumTimePassedInSec * 1000)); - }; - setTimeout(this.checkPost, 1000); - if (threshold) { - this.threshold = threshold; - } - } - appendMessage(msg) { - if (this.threshold && msg.level < this.threshold) { - return; - } - this.msgBuffer.push({ - level: log_message_1.LogLevel[msg.level], - message: typeof msg.message === 'string' - ? msg.message - : JSON.stringify(msg.message), - scope: msg.scope, - stacktrace: msg.stacktrace, - timestamp: msg.timestamp.toISOString() - }); - } - doPost() { - if (this.msgBuffer.length > 0 && this.authToken) { - this.http.post(this.apiEndpoint, this.msgBuffer, { - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${this.authToken}` - } - }); - this.lastSent = Date.now(); - this.msgBuffer = []; - } - } -} -exports.ApiLogAppender = ApiLogAppender; -exports.default = ApiLogAppender; diff --git a/dist/console-log-appender.d.ts b/dist/console-log-appender.d.ts deleted file mode 100644 index 76806d2..0000000 --- a/dist/console-log-appender.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { LogMessage, LogLevel } from './log-message'; -import LogAppender from './log-appender'; -export declare class ConsoleLogAppender implements LogAppender { - threshold: LogLevel; - constructor(threshold?: LogLevel); - appendMessage(msg: LogMessage): void; -} -export default ConsoleLogAppender; diff --git a/dist/console-log-appender.js b/dist/console-log-appender.js deleted file mode 100644 index f29ecab..0000000 --- a/dist/console-log-appender.js +++ /dev/null @@ -1,54 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.ConsoleLogAppender = void 0; -/*tslint:disable:no-console*/ -const log_message_1 = require("./log-message"); -class ConsoleLogAppender { - constructor(threshold) { - this.threshold = log_message_1.LogLevel.ALL; - if (threshold) { - this.threshold = threshold; - } - } - appendMessage(msg) { - if (this.threshold && msg.level < this.threshold) { - return; - } - let logMethod = console.log; - switch (msg.level) { - case log_message_1.LogLevel.ALL: - logMethod = console.log; - break; - case log_message_1.LogLevel.TRACE: - logMethod = console.log; - break; - case log_message_1.LogLevel.DEBUG: - logMethod = console.debug; - break; - case log_message_1.LogLevel.LOG: - logMethod = console.log; - break; - case log_message_1.LogLevel.INFO: - logMethod = console.info; - break; - case log_message_1.LogLevel.WARN: - logMethod = console.warn; - break; - case log_message_1.LogLevel.ERROR: - case log_message_1.LogLevel.FATAL: - logMethod = console.trace; - break; - } - if (msg.error) { - logMethod(`[${msg.scope}]:`, msg.message, msg.error); - } - else if (msg.stacktrace) { - logMethod(`[${msg.scope}]:`, msg.message, msg.stacktrace); - } - else { - logMethod(`[${msg.scope}]:`, msg.message); - } - } -} -exports.ConsoleLogAppender = ConsoleLogAppender; -exports.default = ConsoleLogAppender; diff --git a/dist/index.d.ts b/dist/index.d.ts deleted file mode 100644 index aa1bc7f..0000000 --- a/dist/index.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -export * from './log-message'; -export * from './log-appender'; -export * from './log-service'; -export * from './console-log-appender'; -export * from './api-log-appender'; -export * from './logger'; diff --git a/dist/index.js b/dist/index.js deleted file mode 100644 index e607971..0000000 --- a/dist/index.js +++ /dev/null @@ -1,18 +0,0 @@ -"use strict"; -var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); -}) : (function(o, m, k, k2) { - if (k2 === undefined) k2 = k; - o[k2] = m[k]; -})); -var __exportStar = (this && this.__exportStar) || function(m, exports) { - for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); -}; -Object.defineProperty(exports, "__esModule", { value: true }); -__exportStar(require("./log-message"), exports); -__exportStar(require("./log-appender"), exports); -__exportStar(require("./log-service"), exports); -__exportStar(require("./console-log-appender"), exports); -__exportStar(require("./api-log-appender"), exports); -__exportStar(require("./logger"), exports); diff --git a/dist/log-appender.d.ts b/dist/log-appender.d.ts deleted file mode 100644 index 0233a62..0000000 --- a/dist/log-appender.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { LogLevel, LogMessage } from './log-message'; -export default interface LogAppender { - threshold: LogLevel; - appendMessage(message: LogMessage): void; -} diff --git a/dist/log-appender.js b/dist/log-appender.js deleted file mode 100644 index c8ad2e5..0000000 --- a/dist/log-appender.js +++ /dev/null @@ -1,2 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); diff --git a/dist/log-message.d.ts b/dist/log-message.d.ts deleted file mode 100644 index 6e5f8bd..0000000 --- a/dist/log-message.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -export declare enum LogLevel { - ALL = 0, - TRACE = 1, - DEBUG = 2, - LOG = 3, - INFO = 4, - WARN = 5, - ERROR = 6, - FATAL = 7 -} -export declare function parseLogLevel(str: string, defaultLevel?: LogLevel): LogLevel; -export interface LogMessage { - scope: string; - level: LogLevel; - message: string | object; - stacktrace: string; - error?: Error; - timestamp: Date; -} -export default LogMessage; diff --git a/dist/log-message.js b/dist/log-message.js deleted file mode 100644 index adbb4f6..0000000 --- a/dist/log-message.js +++ /dev/null @@ -1,23 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.parseLogLevel = exports.LogLevel = void 0; -var LogLevel; -(function (LogLevel) { - LogLevel[LogLevel["ALL"] = 0] = "ALL"; - LogLevel[LogLevel["TRACE"] = 1] = "TRACE"; - LogLevel[LogLevel["DEBUG"] = 2] = "DEBUG"; - LogLevel[LogLevel["LOG"] = 3] = "LOG"; - LogLevel[LogLevel["INFO"] = 4] = "INFO"; - LogLevel[LogLevel["WARN"] = 5] = "WARN"; - LogLevel[LogLevel["ERROR"] = 6] = "ERROR"; - LogLevel[LogLevel["FATAL"] = 7] = "FATAL"; -})(LogLevel = exports.LogLevel || (exports.LogLevel = {})); -function parseLogLevel(str, defaultLevel = LogLevel.INFO) { - if (Object.prototype.hasOwnProperty.call(LogLevel, str)) { - return LogLevel[str]; - } - else { - return defaultLevel; - } -} -exports.parseLogLevel = parseLogLevel; diff --git a/dist/log-service.d.ts b/dist/log-service.d.ts deleted file mode 100644 index 4da06f0..0000000 --- a/dist/log-service.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { LogLevel } from './log-message'; -import Logger from './logger'; -export declare class LogService { - private loggers; - get ROOT_LOGGER(): Logger; - constructor(); - getLogger(name: string, threshold?: LogLevel): Logger; -} -export declare const logService: LogService; -export default logService; diff --git a/dist/log-service.js b/dist/log-service.js deleted file mode 100644 index 0452224..0000000 --- a/dist/log-service.js +++ /dev/null @@ -1,35 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.logService = exports.LogService = void 0; -const log_message_1 = require("./log-message"); -const logger_1 = require("./logger"); -const ROOT_LOGGER_NAME = 'ROOT'; -class LogService { - constructor() { - this.loggers = {}; - this.loggers[ROOT_LOGGER_NAME] = new logger_1.default(ROOT_LOGGER_NAME, undefined, log_message_1.LogLevel.ALL); - } - get ROOT_LOGGER() { - return this.loggers[ROOT_LOGGER_NAME]; - } - getLogger(name, threshold) { - if (this.loggers[name]) { - return this.loggers[name]; - } - let parentLogger; - const parentLoggerName = Object.keys(this.loggers) - .filter((n) => name.startsWith(n)) - .reduce((acc, cur) => (acc.length > cur.length ? acc : cur), ''); - if (parentLoggerName) { - parentLogger = this.loggers[parentLoggerName]; - } - else { - parentLogger = this.ROOT_LOGGER; - } - this.loggers[name] = parentLogger.createChildLogger(name, threshold); - return this.loggers[name]; - } -} -exports.LogService = LogService; -exports.logService = new LogService(); -exports.default = exports.logService; diff --git a/dist/logger.d.ts b/dist/logger.d.ts deleted file mode 100644 index fca1f73..0000000 --- a/dist/logger.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { LogMessage, LogLevel } from './log-message'; -import LogAppender from './log-appender'; -export declare type DeferredMsg = () => string | object; -export declare type MessageType = string | DeferredMsg | object; -export declare class Logger { - readonly name: string; - private parentLogger?; - threshold?: LogLevel | undefined; - appenders: LogAppender[]; - constructor(name: string, parentLogger?: Logger | undefined, threshold?: LogLevel | undefined); - createChildLogger(name: string, threshold?: LogLevel): Logger; - doLog(level: LogLevel, message: Error | MessageType, stacktrace?: string): void; - trace(message: Error | MessageType, stacktrace?: string): void; - debug(message: Error | MessageType, stacktrace?: string): void; - log(message: MessageType, stacktrace?: string): void; - info(message: MessageType, stacktrace?: string): void; - warn(message: MessageType, stacktrace?: string): void; - error(message: Error | MessageType, stacktrace?: string): void; - fatal(message: Error | MessageType, stacktrace?: string): void; - protected sendToAppenders(logMsg: LogMessage): void; - protected getEffectiveThreshold(): LogLevel; -} -export default Logger; diff --git a/dist/logger.js b/dist/logger.js deleted file mode 100644 index f9c2f5d..0000000 --- a/dist/logger.js +++ /dev/null @@ -1,88 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.Logger = void 0; -const log_message_1 = require("./log-message"); -class Logger { - constructor(name, parentLogger, threshold) { - this.name = name; - this.parentLogger = parentLogger; - this.threshold = threshold; - this.appenders = []; - } - createChildLogger(name, threshold) { - return new Logger(name, this, threshold); - } - doLog(level, message, stacktrace) { - if (level < this.getEffectiveThreshold()) { - return; - } - const logMsg = { - scope: this.name, - level, - message: '', - stacktrace: '', - timestamp: new Date() - }; - if (message === undefined || message === null) { - logMsg.message = message; - logMsg.stacktrace = stacktrace == null ? '' : stacktrace; - } - else if (message.call !== undefined) { - logMsg.message = message(); - logMsg.stacktrace = stacktrace == null ? '' : stacktrace; - } - else if (message instanceof Error) { - const error = message; - logMsg.error = error; - logMsg.message = `${error.name}: ${error.message}`; - logMsg.stacktrace = error.stack == null ? '' : error.stack; - } - else { - // string | object - logMsg.message = message; - logMsg.stacktrace = stacktrace == null ? '' : stacktrace; - } - this.sendToAppenders(logMsg); - } - trace(message, stacktrace) { - this.doLog(log_message_1.LogLevel.TRACE, message, stacktrace); - } - debug(message, stacktrace) { - this.doLog(log_message_1.LogLevel.DEBUG, message, stacktrace); - } - log(message, stacktrace) { - this.doLog(log_message_1.LogLevel.LOG, message, stacktrace); - } - info(message, stacktrace) { - this.doLog(log_message_1.LogLevel.INFO, message, stacktrace); - } - warn(message, stacktrace) { - this.doLog(log_message_1.LogLevel.WARN, message, stacktrace); - } - error(message, stacktrace) { - this.doLog(log_message_1.LogLevel.ERROR, message, stacktrace); - } - fatal(message, stacktrace) { - this.doLog(log_message_1.LogLevel.FATAL, message, stacktrace); - } - sendToAppenders(logMsg) { - this.appenders.forEach(app => { - app.appendMessage(logMsg); - }); - if (this.parentLogger) { - this.parentLogger.sendToAppenders(logMsg); - } - } - getEffectiveThreshold() { - if (this.threshold) { - return this.threshold; - } - if (this.parentLogger) { - return this.parentLogger.getEffectiveThreshold(); - } - // should never happen (root logger should always have a threshold - return log_message_1.LogLevel.ALL; - } -} -exports.Logger = Logger; -exports.default = Logger; diff --git a/package.json b/package.json index 44f35dc..a3d0e67 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,13 @@ { "name": "@jdbernard/logging", - "version": "1.1.2", + "version": "1.1.3", "description": "Simple Javascript logging service.", - "main": "src/index.ts", - "module": "dist/index.js", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "files": [ "/dist" ], "scripts": { "build": "tsc --build tsconfig.json", + "prepare": "npm run build", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": {