Compare commits
No commits in common. "main" and "1.0.0" have entirely different histories.
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,5 +1,2 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
dist/
|
|
||||||
*.sw?
|
*.sw?
|
||||||
.lvimrc
|
|
||||||
jdbernard-logging-*.tgz
|
|
||||||
|
166
README.md
166
README.md
@ -1,166 +0,0 @@
|
|||||||
# Overview
|
|
||||||
|
|
||||||
`@jdbernard/logging` implements a simple but powerful logging system for
|
|
||||||
JavaScript applications based on the idea of heirarchical loggers. It heavily
|
|
||||||
inspired by the usage patterns of [log4j][], [logback][], and similar tools in other
|
|
||||||
ecosystems.
|
|
||||||
|
|
||||||
## Getting Started
|
|
||||||
|
|
||||||
Install the package from npm:
|
|
||||||
```bash
|
|
||||||
npm install @jdbernard/logging
|
|
||||||
```
|
|
||||||
|
|
||||||
Then, in your application, you can use the logging system like so:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { logService, ConsoleAppender, LogLevel } from '@jdbernard/logging';
|
|
||||||
|
|
||||||
logService.ROOT_LOGGER.appenders.push(new ConsoleAppender(LogLevel.INFO));
|
|
||||||
const logger = logService.getLogger('example');
|
|
||||||
|
|
||||||
logger.info('Starting application');
|
|
||||||
```
|
|
||||||
|
|
||||||
## Loggers and Appenders
|
|
||||||
|
|
||||||
The logging system is composed of two main components: loggers and appenders.
|
|
||||||
Loggers are used to create log events, which are then passed to the appenders
|
|
||||||
associated with the logger and it's parent. An appender is a function that
|
|
||||||
takes a log event and writes it to some destination, such as the console, a
|
|
||||||
file, or a network socket. Appenders also have a logging level threshold, which
|
|
||||||
determines which log events are acted upon by the appender.
|
|
||||||
|
|
||||||
Loggers are hierarchical, with the hierarchy defined by the logger name. When a
|
|
||||||
log message is generated on a *Logger*, it is sent to any appenders associated
|
|
||||||
with that logger, and then triggered on the parent logger, being sent to any of
|
|
||||||
the parent logger's appenders, and so on up the hierarchy. Similarly, when a
|
|
||||||
logging event is generated, it is filtered by the effective logging level of
|
|
||||||
the logger. If there is no threshold set on the logger, the effective logging
|
|
||||||
level is inherited from the parent logger. This pattern is explained in detail
|
|
||||||
in the [logback documentation][effective logging level] and applies in the same
|
|
||||||
manner to this library.
|
|
||||||
|
|
||||||
Together, the cascading effective logging threshold and the upward propogation
|
|
||||||
of log events to parent appenders allows for simple yet fine-grained control
|
|
||||||
over the logging output of an application.
|
|
||||||
|
|
||||||
## Layouts
|
|
||||||
|
|
||||||
One difference from other logging libraries is the absense of layouts. Layouts
|
|
||||||
are used in many logging libraries to format the log message before it is
|
|
||||||
written to the appender. This requires the assumption of all logs being written
|
|
||||||
to a text-based destination, which is not always the case for us.
|
|
||||||
|
|
||||||
In this library, all logs are internally stored as structured data, defined by
|
|
||||||
the *LogMessage* interface. Notably, the *message* field can be either a string
|
|
||||||
or an object, allowing for the addition of arbitrary fields to be added to the
|
|
||||||
log event.
|
|
||||||
|
|
||||||
The concept of Layouts still has a place, but it has been moved to be an
|
|
||||||
implementation detail of the appenders. *ConsoleAppender* accepts a
|
|
||||||
*LogMessageFormatter* function allowing for formatting of the resulting
|
|
||||||
messages written to the console. the *ApiAppender* would be an example of an
|
|
||||||
appender for which layout would be irrelevant.
|
|
||||||
|
|
||||||
Finally, notice that all of the appenders provided by this library
|
|
||||||
automatically persist log messages as structured data, flattening the `message`
|
|
||||||
field if it is an object. *ConsoleAppender* will write the log message as a
|
|
||||||
JSON object, promoting fields in the `message` object to top-level fields in
|
|
||||||
the output. For example:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { logService, ConsoleAppender } from '@jdbernard/logging';
|
|
||||||
|
|
||||||
logService.ROOT_LOGGER.appenders.push(new ConsoleAppender('TRACE'));
|
|
||||||
const logger = logService.getLogger('example');
|
|
||||||
|
|
||||||
function someBusinessLogic() {
|
|
||||||
logger.debug({ method: 'someBusinessLogic', msg: 'Doing some business logic', foo: 'bar' });
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info('Starting application');
|
|
||||||
someBusinessLogic();
|
|
||||||
```
|
|
||||||
|
|
||||||
results in the following two events logged as output to the console:
|
|
||||||
|
|
||||||
```json
|
|
||||||
{"timestamp":"2021-09-01T00:00:00.000Z","level":"INFO","scope":"example","message":"Starting application"}
|
|
||||||
{"timestamp":"2021-09-01T00:00:00.000Z","level":"DEBUG","scope":"example","msg":"Doing some business logic","method":"someBusinessLogic","foo":"bar"}
|
|
||||||
```
|
|
||||||
|
|
||||||
Note that the field name in the structured data for string messages is
|
|
||||||
"message". In the case of an object message, the fields are used as provided.
|
|
||||||
Fields defined on the *LogMessage* interface are reserved and should not be
|
|
||||||
used as keys in the message object (and will be ignored if they are). So, for
|
|
||||||
example:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
logger.debug({ level: 'WARN', message: 'Example of ignored fields', timestamp: 'today' });
|
|
||||||
|
|
||||||
results in the following event logged as output to the console (note the
|
|
||||||
ignored `level` and `timestamp` fields from the object):
|
|
||||||
|
|
||||||
```json
|
|
||||||
{"timestamp":"2021-09-01T00:00:00.000Z","level":"DEBUG","scope":"example","message":"Example of ignored fields"}
|
|
||||||
```
|
|
||||||
|
|
||||||
This flattening behavior is implemented in the `flattenMessage` function
|
|
||||||
exported from the `@jdbernard/logging/log-message.ts` module and is available
|
|
||||||
for use in custom appender implemetations.
|
|
||||||
|
|
||||||
As a final note, the *ConsoleAppender* has some special behavior for the ERROR
|
|
||||||
level and *stacktrace* field when running in a web browser to take advantage of
|
|
||||||
the console's built-in. See the documentation for the *ConsoleAppender* for
|
|
||||||
more information.
|
|
||||||
|
|
||||||
## Worked Example
|
|
||||||
|
|
||||||
To illustrate the usage of the logging system and the purpose of the
|
|
||||||
fine-grained control described above, consider the following TypeScript
|
|
||||||
example:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
// main.ts -- Entry point of the application
|
|
||||||
// A console appender is attached to the root logger to log all events at or
|
|
||||||
// above the INFO level.
|
|
||||||
logService.ROOT_LOGGER.appenders.push(new ConsoleAppender(LogLevel.INFO));
|
|
||||||
|
|
||||||
// An API appender is attached to the root logger to forward all ERROR events
|
|
||||||
// to a server-side error log collector.
|
|
||||||
logServive.ROOT_LOGGER.appenders.push(new ApiAppender(
|
|
||||||
'https://api.example.com/error-logs',
|
|
||||||
appStore.user.sessionToken,
|
|
||||||
LogLevel.ERROR));
|
|
||||||
|
|
||||||
const appLog = logService.getLogger('app');
|
|
||||||
appLog.info('Starting application');
|
|
||||||
|
|
||||||
// api.ts -- API implementaiton
|
|
||||||
const apiLog = logService.getLogger('app/api');
|
|
||||||
|
|
||||||
// http-client.ts -- HTTP client implementation
|
|
||||||
const httpLog = logService.getLogger('app/api/http-client');
|
|
||||||
```
|
|
||||||
|
|
||||||
Because different parts of the application use namespaced loggers, we can
|
|
||||||
dynamically adjust the logging level of different parts of the application
|
|
||||||
without changing the code. For example, to increase the logging level of the
|
|
||||||
HTTP client to DEBUG, we can add the following line to the `main.ts` file:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
logService.getLogger('app/api/http-client').setLevel(LogLevel.DEBUG);
|
|
||||||
```
|
|
||||||
|
|
||||||
Additionally, if, for some reason, we only wanted to forward the ERROR events
|
|
||||||
from the API namespace to the server-side error log collector, we could attach
|
|
||||||
the *ApiAppender* to the `app/api` logger instead of the root logger. This
|
|
||||||
would allow us to log all events from both the API and HTTP client loggers to
|
|
||||||
the API log collector, but ignore the rest of the application regardless of the
|
|
||||||
logging level.
|
|
||||||
|
|
||||||
[log4j]: https://logging.apache.org/log4j/2.x/
|
|
||||||
[logback]: https://logback.qos.ch/
|
|
||||||
[effective logging level]: https://logback.qos.ch/manual/architecture.html#effectiveLevel
|
|
18
dist/api-log-appender.d.ts
vendored
Normal file
18
dist/api-log-appender.d.ts
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
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
Normal file
60
dist/api-log-appender.js
vendored
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
"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
Normal file
8
dist/console-log-appender.d.ts
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
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
Normal file
54
dist/console-log-appender.js
vendored
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
"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
Normal file
6
dist/index.d.ts
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
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
Normal file
18
dist/index.js
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
"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
Normal file
5
dist/log-appender.d.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { LogLevel, LogMessage } from './log-message';
|
||||||
|
export default interface LogAppender {
|
||||||
|
threshold: LogLevel;
|
||||||
|
appendMessage(message: LogMessage): void;
|
||||||
|
}
|
2
dist/log-appender.js
vendored
Normal file
2
dist/log-appender.js
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
19
dist/log-message.d.ts
vendored
Normal file
19
dist/log-message.d.ts
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
export declare enum LogLevel {
|
||||||
|
ALL = 0,
|
||||||
|
TRACE = 1,
|
||||||
|
DEBUG = 2,
|
||||||
|
LOG = 3,
|
||||||
|
INFO = 4,
|
||||||
|
WARN = 5,
|
||||||
|
ERROR = 6,
|
||||||
|
FATAL = 7
|
||||||
|
}
|
||||||
|
export interface LogMessage {
|
||||||
|
scope: string;
|
||||||
|
level: LogLevel;
|
||||||
|
message: string | object;
|
||||||
|
stacktrace: string;
|
||||||
|
error?: Error;
|
||||||
|
timestamp: Date;
|
||||||
|
}
|
||||||
|
export default LogMessage;
|
14
dist/log-message.js
vendored
Normal file
14
dist/log-message.js
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
"use strict";
|
||||||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
|
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 = {}));
|
10
dist/log-service.d.ts
vendored
Normal file
10
dist/log-service.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
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
Normal file
35
dist/log-service.js
vendored
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
"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
Normal file
23
dist/logger.d.ts
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
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
Normal file
88
dist/logger.js
vendored
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
"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,68 +0,0 @@
|
|||||||
import importPlugin from 'eslint-plugin-import'
|
|
||||||
import tsParser from '@typescript-eslint/parser'
|
|
||||||
import eslintJs from '@eslint/js'
|
|
||||||
import eslintTs from 'typescript-eslint'
|
|
||||||
|
|
||||||
const tsFiles = ['src/**/*.ts']
|
|
||||||
|
|
||||||
const customTypescriptConfig = {
|
|
||||||
files: tsFiles,
|
|
||||||
plugins: {
|
|
||||||
import: importPlugin,
|
|
||||||
'import/parsers': tsParser,
|
|
||||||
},
|
|
||||||
languageOptions: {
|
|
||||||
parser: tsParser,
|
|
||||||
parserOptions: {
|
|
||||||
project: './tsconfig.json',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
settings: {
|
|
||||||
'import/parsers': {
|
|
||||||
'@typescript-eslint/parser': ['.ts'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
rules: {
|
|
||||||
'linebreak-style': ['error', 'unix'],
|
|
||||||
quotes: ['error', 'single', { avoidEscape: true }],
|
|
||||||
semi: ['error', 'never'],
|
|
||||||
'@typescript-eslint/no-unused-vars': ['error', {
|
|
||||||
'args': 'all',
|
|
||||||
'argsIgnorePattern': '^_',
|
|
||||||
'caughtErrorsIgnorePattern': '^_',
|
|
||||||
'destructuredArrayIgnorePattern': '^_',
|
|
||||||
'varsIgnorePattern': '^_',
|
|
||||||
}],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
const recommendedTypeScriptConfigs = [
|
|
||||||
...eslintTs.configs.recommended.map((config) => ({
|
|
||||||
...config,
|
|
||||||
files: tsFiles,
|
|
||||||
})),
|
|
||||||
...eslintTs.configs.stylistic.map((config) => ({
|
|
||||||
...config,
|
|
||||||
files: tsFiles,
|
|
||||||
})),
|
|
||||||
]
|
|
||||||
|
|
||||||
export default [
|
|
||||||
{
|
|
||||||
ignores: [
|
|
||||||
'docs/*',
|
|
||||||
'build/*',
|
|
||||||
'lib/*',
|
|
||||||
'dist/*',
|
|
||||||
],
|
|
||||||
}, // global ignores
|
|
||||||
eslintJs.configs.recommended,
|
|
||||||
...recommendedTypeScriptConfigs,
|
|
||||||
customTypescriptConfig,
|
|
||||||
{
|
|
||||||
rules: {
|
|
||||||
quotes: ['error', 'single', { avoidEscape: true }],
|
|
||||||
semi: ['error', 'never'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
]
|
|
43
package-lock.json
generated
Normal file
43
package-lock.json
generated
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
{
|
||||||
|
"name": "js-log",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"lockfileVersion": 1,
|
||||||
|
"requires": true,
|
||||||
|
"dependencies": {
|
||||||
|
"axios": {
|
||||||
|
"version": "0.19.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz",
|
||||||
|
"integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==",
|
||||||
|
"requires": {
|
||||||
|
"follow-redirects": "1.5.10"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"debug": {
|
||||||
|
"version": "3.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
|
||||||
|
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
|
||||||
|
"requires": {
|
||||||
|
"ms": "2.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"follow-redirects": {
|
||||||
|
"version": "1.5.10",
|
||||||
|
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz",
|
||||||
|
"integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==",
|
||||||
|
"requires": {
|
||||||
|
"debug": "=3.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"ms": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||||
|
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||||
|
},
|
||||||
|
"typescript": {
|
||||||
|
"version": "3.9.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.9.7.tgz",
|
||||||
|
"integrity": "sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw==",
|
||||||
|
"dev": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
21
package.json
21
package.json
@ -1,20 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "@jdbernard/logging",
|
"name": "@jdbernard/logging",
|
||||||
"version": "2.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Simple Javascript logging service.",
|
"description": "Simple Javascript logging service.",
|
||||||
"main": "dist/index.js",
|
"main": "src/index.ts",
|
||||||
"types": "dist/index.d.ts",
|
"module": "dist/index.js",
|
||||||
"files": [
|
|
||||||
"/dist"
|
|
||||||
],
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
|
||||||
"prepare": "npm run build",
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://git.jdb-software.com/jdb/js-logging.git"
|
"url": "https://git.jdb-labs.com/jdb/js-logging.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"log",
|
"log",
|
||||||
@ -23,9 +18,9 @@
|
|||||||
"author": "Jonathan Bernard",
|
"author": "Jonathan Bernard",
|
||||||
"license": "GPL-3.0",
|
"license": "GPL-3.0",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@typescript-eslint/parser": "^8.19.0",
|
"typescript": "^3.9.7"
|
||||||
"eslint-plugin-import": "^2.31.0",
|
},
|
||||||
"typescript": "^5.0.4",
|
"dependencies": {
|
||||||
"typescript-eslint": "^8.19.0"
|
"axios": "^0.19.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,23 @@
|
|||||||
import { LogMessage, LogLevel, flattenMessage, FlattenedLogMessage } from './log-message';
|
import Axios from 'axios';
|
||||||
import { LogAppender } from './log-appender';
|
|
||||||
|
|
||||||
|
import { LogMessage, LogLevel } from './log-message';
|
||||||
|
import LogAppender from './log-appender';
|
||||||
|
|
||||||
|
interface ApiMessage {
|
||||||
|
level: string;
|
||||||
|
message: string;
|
||||||
|
scope: string;
|
||||||
|
stacktrace: string;
|
||||||
|
timestamp: string;
|
||||||
|
}
|
||||||
export class ApiLogAppender implements LogAppender {
|
export class ApiLogAppender implements LogAppender {
|
||||||
public batchSize = 10;
|
public batchSize = 10;
|
||||||
public minimumTimePassedInSec = 60;
|
public minimumTimePassedInSec = 60;
|
||||||
public maximumTimePassedInSec = 120;
|
public maximumTimePassedInSec = 120;
|
||||||
public threshold = LogLevel.ALL;
|
public threshold = LogLevel.ALL;
|
||||||
|
|
||||||
private msgBuffer: FlattenedLogMessage[] = [];
|
private http = Axios.create();
|
||||||
|
private msgBuffer: ApiMessage[] = [];
|
||||||
private lastSent = 0;
|
private lastSent = 0;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@ -26,15 +36,21 @@ export class ApiLogAppender implements LogAppender {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.msgBuffer.push(flattenMessage(msg));
|
this.msgBuffer.push({
|
||||||
|
level: LogLevel[msg.level],
|
||||||
|
message:
|
||||||
|
typeof msg.message === 'string'
|
||||||
|
? msg.message
|
||||||
|
: JSON.stringify(msg.message),
|
||||||
|
scope: msg.scope,
|
||||||
|
stacktrace: msg.stacktrace,
|
||||||
|
timestamp: msg.timestamp.toISOString()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private doPost() {
|
private doPost() {
|
||||||
if (this.msgBuffer.length > 0 && this.authToken) {
|
if (this.msgBuffer.length > 0 && this.authToken) {
|
||||||
fetch(this.apiEndpoint, {
|
this.http.post(this.apiEndpoint, this.msgBuffer, {
|
||||||
method: 'POST',
|
|
||||||
mode: 'cors',
|
|
||||||
body: JSON.stringify(this.msgBuffer),
|
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
Authorization: `Bearer ${this.authToken}`
|
Authorization: `Bearer ${this.authToken}`
|
||||||
|
@ -1,33 +1,14 @@
|
|||||||
import {
|
/*tslint:disable:no-console*/
|
||||||
flattenMessage,
|
import { LogMessage, LogLevel } from './log-message';
|
||||||
LogLevel,
|
import LogAppender from './log-appender';
|
||||||
LogMessage,
|
|
||||||
LogMessageFormatter,
|
|
||||||
} from "./log-message";
|
|
||||||
import { LogAppender } from "./log-appender";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A log appender that writes log messages to the console. The behavior of the
|
|
||||||
* log appender can be configured with a threshold level and a message
|
|
||||||
* formatter.
|
|
||||||
*
|
|
||||||
* When the message formatter returns a string value, that value is logged to
|
|
||||||
* the console as-is. When the message formatter returns an object, a summary
|
|
||||||
* string is logged to the console, followed by the object itself. This allows
|
|
||||||
* logs to be easily read in the console, while still providing the structured
|
|
||||||
* data for inspection in the browser's developer tools.
|
|
||||||
*/
|
|
||||||
export class ConsoleLogAppender implements LogAppender {
|
export class ConsoleLogAppender implements LogAppender {
|
||||||
public threshold = LogLevel.ALL;
|
public threshold = LogLevel.ALL;
|
||||||
public formatter: LogMessageFormatter = flattenMessage;
|
|
||||||
|
|
||||||
constructor(threshold?: LogLevel, formatter?: LogMessageFormatter) {
|
constructor(threshold?: LogLevel) {
|
||||||
if (threshold) {
|
if (threshold) {
|
||||||
this.threshold = threshold;
|
this.threshold = threshold;
|
||||||
}
|
}
|
||||||
if (formatter) {
|
|
||||||
this.formatter = formatter;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public appendMessage(msg: LogMessage): void {
|
public appendMessage(msg: LogMessage): void {
|
||||||
@ -38,13 +19,17 @@ export class ConsoleLogAppender implements LogAppender {
|
|||||||
let logMethod = console.log;
|
let logMethod = console.log;
|
||||||
switch (msg.level) {
|
switch (msg.level) {
|
||||||
case LogLevel.ALL:
|
case LogLevel.ALL:
|
||||||
|
logMethod = console.log;
|
||||||
|
break;
|
||||||
case LogLevel.TRACE:
|
case LogLevel.TRACE:
|
||||||
case LogLevel.LOG:
|
|
||||||
logMethod = console.log;
|
logMethod = console.log;
|
||||||
break;
|
break;
|
||||||
case LogLevel.DEBUG:
|
case LogLevel.DEBUG:
|
||||||
logMethod = console.debug;
|
logMethod = console.debug;
|
||||||
break;
|
break;
|
||||||
|
case LogLevel.LOG:
|
||||||
|
logMethod = console.log;
|
||||||
|
break;
|
||||||
case LogLevel.INFO:
|
case LogLevel.INFO:
|
||||||
logMethod = console.info;
|
logMethod = console.info;
|
||||||
break;
|
break;
|
||||||
@ -57,25 +42,12 @@ export class ConsoleLogAppender implements LogAppender {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fmtMsg = this.formatter(msg);
|
if (msg.error) {
|
||||||
|
logMethod(`[${msg.scope}]:`, msg.message, msg.error);
|
||||||
if (typeof fmtMsg === "string") {
|
} else if (msg.stacktrace) {
|
||||||
if (msg.error || msg.stacktrace) {
|
logMethod(`[${msg.scope}]:`, msg.message, msg.stacktrace);
|
||||||
logMethod(fmtMsg, msg.error ?? msg.stacktrace);
|
|
||||||
} else {
|
|
||||||
logMethod(fmtMsg);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
const { message, _error, _stacktrace, ...rest } = fmtMsg;
|
logMethod(`[${msg.scope}]:`, msg.message);
|
||||||
const summary = `${LogLevel[msg.level]} -- ${msg.scope}: ${
|
|
||||||
message ?? fmtMsg.method
|
|
||||||
}\n`;
|
|
||||||
|
|
||||||
if (msg.error || msg.stacktrace) {
|
|
||||||
logMethod(summary, msg.error ?? msg.stacktrace, rest);
|
|
||||||
} else {
|
|
||||||
logMethod(summary, rest);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
import { LogLevel, LogMessage } from './log-message';
|
import { LogLevel, LogMessage } from './log-message';
|
||||||
|
export default interface LogAppender {
|
||||||
export interface LogAppender {
|
|
||||||
threshold: LogLevel;
|
threshold: LogLevel;
|
||||||
appendMessage(message: LogMessage): void;
|
appendMessage(message: LogMessage): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default LogAppender;
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
import { omit } from './util'
|
|
||||||
|
|
||||||
export enum LogLevel {
|
export enum LogLevel {
|
||||||
ALL = 0,
|
ALL = 0,
|
||||||
TRACE,
|
TRACE,
|
||||||
@ -8,77 +6,16 @@ export enum LogLevel {
|
|||||||
INFO,
|
INFO,
|
||||||
WARN,
|
WARN,
|
||||||
ERROR,
|
ERROR,
|
||||||
FATAL,
|
FATAL
|
||||||
}
|
|
||||||
|
|
||||||
export function parseLogLevel(
|
|
||||||
str: string,
|
|
||||||
defaultLevel = LogLevel.INFO,
|
|
||||||
): LogLevel {
|
|
||||||
if (str in LogLevel) {
|
|
||||||
return LogLevel[str as keyof typeof LogLevel] as LogLevel;
|
|
||||||
} else {
|
|
||||||
return defaultLevel;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LogMessage {
|
export interface LogMessage {
|
||||||
scope: string;
|
scope: string;
|
||||||
level: LogLevel;
|
level: LogLevel;
|
||||||
message: string | Record<string, unknown>;
|
message: string | object;
|
||||||
stacktrace?: string;
|
stacktrace: string;
|
||||||
error?: Error;
|
error?: Error;
|
||||||
timestamp: Date;
|
timestamp: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FlattenedLogMessage = Record<string, unknown>;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flatten a log message to a plain object. The *message* field can be either a
|
|
||||||
* string or an object. In the case of an object message, the LogMessage should
|
|
||||||
* be flattened before being emitted by an appender, promoting the object's
|
|
||||||
* fields to the top level of the message. Fields defined on the *LogMessage*
|
|
||||||
* interface are reserved and should not be used as keys in the message object
|
|
||||||
* (and will be ignored if they are).
|
|
||||||
*
|
|
||||||
* So, for example:
|
|
||||||
*
|
|
||||||
* ```typescript
|
|
||||||
* const logger = logService.getLogger('example');
|
|
||||||
* logger.info({ foo: 'bar', baz: 'qux', timestamp: 'today', level: LogLevel.WARN });
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* Should result after flattening in a structured log message like:
|
|
||||||
* ```json
|
|
||||||
* {"scope":"example","level":"INFO","foo":"bar","baz":"qux","timestamp":"2020-01-01T00:00:00.000Z"}
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
export function flattenMessage(msg: LogMessage): FlattenedLogMessage {
|
|
||||||
if (typeof msg.message === 'string') {
|
|
||||||
return { ...msg, level: LogLevel[msg.level] };
|
|
||||||
} else {
|
|
||||||
const { message, ...rest } = msg;
|
|
||||||
return {
|
|
||||||
...omit(message, [
|
|
||||||
'scope',
|
|
||||||
'level',
|
|
||||||
'stacktrace',
|
|
||||||
'error',
|
|
||||||
'timestamp',
|
|
||||||
]),
|
|
||||||
...rest,
|
|
||||||
level: LogLevel[msg.level],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export type LogMessageFormatter = (msg: LogMessage) => string | FlattenedLogMessage;
|
|
||||||
|
|
||||||
export function structuredLogMessageFormatter(msg: LogMessage): string {
|
|
||||||
return JSON.stringify(flattenMessage(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function simpleTextLogMessageFormatter(msg: LogMessage): string {
|
|
||||||
return `[${msg.scope}] - ${msg.level}: ${msg.message}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default LogMessage;
|
export default LogMessage;
|
||||||
|
@ -1,16 +1,10 @@
|
|||||||
import { LogLevel } from './log-message';
|
import { LogLevel } from './log-message';
|
||||||
import { Logger } from './logger';
|
import Logger from './logger';
|
||||||
|
|
||||||
const ROOT_LOGGER_NAME = 'ROOT';
|
const ROOT_LOGGER_NAME = 'ROOT';
|
||||||
|
|
||||||
/**
|
|
||||||
* Service for managing loggers. A LogService instance defines
|
|
||||||
* the service context for a set of loggers. Typically there is only one
|
|
||||||
* LogService instance per application, the one exported as default from this
|
|
||||||
* module.
|
|
||||||
*/
|
|
||||||
export class LogService {
|
export class LogService {
|
||||||
private loggers: Record<string, Logger>;
|
private loggers: { [key: string]: Logger };
|
||||||
|
|
||||||
public get ROOT_LOGGER() {
|
public get ROOT_LOGGER() {
|
||||||
return this.loggers[ROOT_LOGGER_NAME];
|
return this.loggers[ROOT_LOGGER_NAME];
|
||||||
@ -21,38 +15,10 @@ export class LogService {
|
|||||||
this.loggers[ROOT_LOGGER_NAME] = new Logger(
|
this.loggers[ROOT_LOGGER_NAME] = new Logger(
|
||||||
ROOT_LOGGER_NAME,
|
ROOT_LOGGER_NAME,
|
||||||
undefined,
|
undefined,
|
||||||
LogLevel.ALL,
|
LogLevel.ALL
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get a logger by name. If the logger does not exist, it will be created.
|
|
||||||
* Loggers are hierarchical, with the heirarchy defined by the logger name.
|
|
||||||
* When creating a new logger, the parent logger is determined by the longest
|
|
||||||
* existing logger name that is a prefix of the new logger name. For example,
|
|
||||||
* if a logger with the name "foo" already exists, any subsequent loggers
|
|
||||||
* with the names "foo.bar", "foo/bar", "foolish.choice", etc. will be
|
|
||||||
* created as children of the "foo" logger.
|
|
||||||
*
|
|
||||||
* As another example, given the following invocations:
|
|
||||||
*
|
|
||||||
* ```typescript
|
|
||||||
* logService.getLogger('foo');
|
|
||||||
* logService.getLogger('foo.bar');
|
|
||||||
* logService.getLogger('foo.bar.baz');
|
|
||||||
* logService.getLogger('foo.qux');
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
* will result in the following logging hierarchy:
|
|
||||||
*
|
|
||||||
* foo
|
|
||||||
* ├─foo.bar
|
|
||||||
* │ └─foo.bar.baz
|
|
||||||
* └─foo.qux
|
|
||||||
*
|
|
||||||
* See the Logger class for details on how loggers are used and the
|
|
||||||
* implications of the logger hierarchy.
|
|
||||||
*/
|
|
||||||
public getLogger(name: string, threshold?: LogLevel): Logger {
|
public getLogger(name: string, threshold?: LogLevel): Logger {
|
||||||
if (this.loggers[name]) {
|
if (this.loggers[name]) {
|
||||||
return this.loggers[name];
|
return this.loggers[name];
|
||||||
@ -64,7 +30,7 @@ export class LogService {
|
|||||||
.filter((n: string) => name.startsWith(n))
|
.filter((n: string) => name.startsWith(n))
|
||||||
.reduce(
|
.reduce(
|
||||||
(acc: string, cur: string) => (acc.length > cur.length ? acc : cur),
|
(acc: string, cur: string) => (acc.length > cur.length ? acc : cur),
|
||||||
'',
|
''
|
||||||
);
|
);
|
||||||
|
|
||||||
if (parentLoggerName) {
|
if (parentLoggerName) {
|
||||||
|
@ -1,52 +1,16 @@
|
|||||||
import { LogLevel, LogMessage } from './log-message';
|
import { LogMessage, LogLevel } from './log-message';
|
||||||
import { LogAppender } from './log-appender';
|
import LogAppender from './log-appender';
|
||||||
|
|
||||||
export type DeferredMsg = () => string | Record<string, unknown>;
|
export type DeferredMsg = () => string | object;
|
||||||
export type MessageType = string | DeferredMsg | Record<string, unknown>;
|
export type MessageType = string | DeferredMsg | object;
|
||||||
|
|
||||||
export function isDeferredMsg(msg: MessageType): msg is DeferredMsg {
|
|
||||||
return typeof msg === 'function';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Logger class for logging messages.
|
|
||||||
*
|
|
||||||
* Loggers are used to log messages at different levels. The levels are, in
|
|
||||||
* order of increasing severity: TRACE, DEBUG, LOG, INFO, WARN, ERROR, FATAL.
|
|
||||||
* Log messages can be logged at a specific level by calling the corresponding
|
|
||||||
* method on the logger instance. For example, to log a message at the INFO
|
|
||||||
* level, call the *info* method on the logger.
|
|
||||||
*
|
|
||||||
* Loggers have a threshold level, which is the minimum level of log message
|
|
||||||
* that will be logged by the logger. Log messages with a level below the
|
|
||||||
* threshold will be ignored. The threshold level can be set when creating a
|
|
||||||
* logger, or by calling the *setThreshold* method on an existing logger. If a
|
|
||||||
* threshold is not set, the logger will use the threshold of its parent
|
|
||||||
* logger.
|
|
||||||
*
|
|
||||||
* Loggers are hierarchical, with the hierarchy defined by the logger name.
|
|
||||||
* The heirarchy is tracked by the children: child loggers have a link to their
|
|
||||||
* parents, while parent loggers do not have a list of their children.
|
|
||||||
*
|
|
||||||
* When a log message is logged by a logger, it is sent to all of the appenders
|
|
||||||
* attached to the logger and to the parent logger. Appenders can be attached
|
|
||||||
* to any Logger instance, but, because of this upwards propogation, appenders
|
|
||||||
* are usually attached to the root logger, which is an ancestor of all loggers
|
|
||||||
* created by the same LogService instance.
|
|
||||||
*
|
|
||||||
* Loggers are typically created and managed by a LogService instance. The
|
|
||||||
* *LogService#getLogger* method is used to get a logger by name, creating it
|
|
||||||
* if necessary. When creating a new logger, the parent logger is determined by
|
|
||||||
* the longest existing logger name that is a prefix of the new logger name.
|
|
||||||
* For more details, see *LogService#getLogger*.
|
|
||||||
*/
|
|
||||||
export class Logger {
|
export class Logger {
|
||||||
public appenders: LogAppender[] = [];
|
public appenders: LogAppender[] = [];
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
public readonly name: string,
|
public readonly name: string,
|
||||||
private parentLogger?: Logger,
|
private parentLogger?: Logger,
|
||||||
public threshold?: LogLevel,
|
public threshold?: LogLevel
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public createChildLogger(name: string, threshold?: LogLevel): Logger {
|
public createChildLogger(name: string, threshold?: LogLevel): Logger {
|
||||||
@ -56,7 +20,7 @@ export class Logger {
|
|||||||
public doLog(
|
public doLog(
|
||||||
level: LogLevel,
|
level: LogLevel,
|
||||||
message: Error | MessageType,
|
message: Error | MessageType,
|
||||||
stacktrace?: string,
|
stacktrace?: string
|
||||||
): void {
|
): void {
|
||||||
if (level < this.getEffectiveThreshold()) {
|
if (level < this.getEffectiveThreshold()) {
|
||||||
return;
|
return;
|
||||||
@ -67,20 +31,20 @@ export class Logger {
|
|||||||
level,
|
level,
|
||||||
message: '',
|
message: '',
|
||||||
stacktrace: '',
|
stacktrace: '',
|
||||||
timestamp: new Date(),
|
timestamp: new Date()
|
||||||
};
|
};
|
||||||
|
|
||||||
if (message === undefined || message === null) {
|
if (message === undefined || message === null) {
|
||||||
logMsg.message = message;
|
logMsg.message = message;
|
||||||
logMsg.stacktrace = stacktrace ?? '';
|
logMsg.stacktrace = stacktrace == null ? '' : stacktrace;
|
||||||
|
} else if ((message as DeferredMsg).call !== undefined) {
|
||||||
|
logMsg.message = (message as DeferredMsg)();
|
||||||
|
logMsg.stacktrace = stacktrace == null ? '' : stacktrace;
|
||||||
} else if (message instanceof Error) {
|
} else if (message instanceof Error) {
|
||||||
const error = message as Error;
|
const error = message as Error;
|
||||||
logMsg.error = error;
|
logMsg.error = error;
|
||||||
logMsg.message = `${error.name}: ${error.message}`;
|
logMsg.message = `${error.name}: ${error.message}`;
|
||||||
logMsg.stacktrace = stacktrace ?? error.stack ?? '';
|
logMsg.stacktrace = error.stack == null ? '' : error.stack;
|
||||||
} else if (isDeferredMsg(message)) {
|
|
||||||
logMsg.message = message();
|
|
||||||
logMsg.stacktrace = stacktrace == null ? '' : stacktrace;
|
|
||||||
} else {
|
} else {
|
||||||
// string | object
|
// string | object
|
||||||
logMsg.message = message;
|
logMsg.message = message;
|
||||||
@ -119,7 +83,7 @@ export class Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected sendToAppenders(logMsg: LogMessage) {
|
protected sendToAppenders(logMsg: LogMessage) {
|
||||||
this.appenders.forEach((app) => {
|
this.appenders.forEach(app => {
|
||||||
app.appendMessage(logMsg);
|
app.appendMessage(logMsg);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
12
src/util.ts
12
src/util.ts
@ -1,12 +0,0 @@
|
|||||||
export function omit(
|
|
||||||
obj: Record<string, unknown>,
|
|
||||||
keys: string[],
|
|
||||||
): Record<string, unknown> {
|
|
||||||
const result: Record<string, unknown> = {};
|
|
||||||
for (const key in obj) {
|
|
||||||
if (!keys.includes(key)) {
|
|
||||||
result[key] = obj[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
@ -1,11 +1,10 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"target": "es2016",
|
"target": "es6",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"strict": true,
|
"strict": true
|
||||||
"skipLibCheck": true
|
|
||||||
},
|
},
|
||||||
"include": [
|
"include": [
|
||||||
"src/**/*"
|
"src/**/*"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user