Fix NPM packaging.
This commit is contained in:
parent
b2d1b71a52
commit
f75edbc22a
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
|
dist/
|
||||||
*.sw?
|
*.sw?
|
||||||
|
18
dist/api-log-appender.d.ts
vendored
18
dist/api-log-appender.d.ts
vendored
@ -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;
|
|
60
dist/api-log-appender.js
vendored
60
dist/api-log-appender.js
vendored
@ -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;
|
|
8
dist/console-log-appender.d.ts
vendored
8
dist/console-log-appender.d.ts
vendored
@ -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;
|
|
54
dist/console-log-appender.js
vendored
54
dist/console-log-appender.js
vendored
@ -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;
|
|
6
dist/index.d.ts
vendored
6
dist/index.d.ts
vendored
@ -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';
|
|
18
dist/index.js
vendored
18
dist/index.js
vendored
@ -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);
|
|
5
dist/log-appender.d.ts
vendored
5
dist/log-appender.d.ts
vendored
@ -1,5 +0,0 @@
|
|||||||
import { LogLevel, LogMessage } from './log-message';
|
|
||||||
export default interface LogAppender {
|
|
||||||
threshold: LogLevel;
|
|
||||||
appendMessage(message: LogMessage): void;
|
|
||||||
}
|
|
2
dist/log-appender.js
vendored
2
dist/log-appender.js
vendored
@ -1,2 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
dist/log-message.d.ts
vendored
20
dist/log-message.d.ts
vendored
@ -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;
|
|
23
dist/log-message.js
vendored
23
dist/log-message.js
vendored
@ -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;
|
|
10
dist/log-service.d.ts
vendored
10
dist/log-service.d.ts
vendored
@ -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;
|
|
35
dist/log-service.js
vendored
35
dist/log-service.js
vendored
@ -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;
|
|
23
dist/logger.d.ts
vendored
23
dist/logger.d.ts
vendored
@ -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;
|
|
88
dist/logger.js
vendored
88
dist/logger.js
vendored
@ -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;
|
|
@ -1,11 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "@jdbernard/logging",
|
"name": "@jdbernard/logging",
|
||||||
"version": "1.1.2",
|
"version": "1.1.3",
|
||||||
"description": "Simple Javascript logging service.",
|
"description": "Simple Javascript logging service.",
|
||||||
"main": "src/index.ts",
|
"main": "dist/index.js",
|
||||||
"module": "dist/index.js",
|
"types": "dist/index.d.ts",
|
||||||
|
"files": [ "/dist" ],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc --build tsconfig.json",
|
"build": "tsc --build tsconfig.json",
|
||||||
|
"prepare": "npm run build",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
|
Loading…
Reference in New Issue
Block a user