2 Commits

4 changed files with 51 additions and 49 deletions

View File

@ -54,7 +54,7 @@ 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. 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 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 the *LogMessage* interface. Notably, the *msg* field can be either a string
or an object, allowing for the addition of arbitrary fields to be added to the or an object, allowing for the addition of arbitrary fields to be added to the
log event. log event.
@ -65,9 +65,9 @@ messages written to the console. the *ApiAppender* would be an example of an
appender for which layout would be irrelevant. appender for which layout would be irrelevant.
Finally, notice that all of the appenders provided by this library Finally, notice that all of the appenders provided by this library
automatically persist log messages as structured data, flattening the `message` automatically persist log messages as structured data, flattening the `msg`
field if it is an object. *ConsoleAppender* will write the log message as a 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 JSON object, promoting fields in the `msg` object to top-level fields in
the output. For example: the output. For example:
```typescript ```typescript
@ -87,24 +87,24 @@ someBusinessLogic();
results in the following two events logged as output to the console: results in the following two events logged as output to the console:
```json ```json
{"timestamp":"2021-09-01T00:00:00.000Z","level":"INFO","scope":"example","message":"Starting application"} {"timestamp":"2021-09-01T00:00:00.000Z","level":"INFO","scope":"example","msg":"Starting application"}
{"timestamp":"2021-09-01T00:00:00.000Z","level":"DEBUG","scope":"example","msg":"Doing some business logic","method":"someBusinessLogic","foo":"bar"} {"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 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. "msg". 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 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 used as keys in the msg object (and will be ignored if they are). So, for
example: example:
```typescript ```typescript
logger.debug({ level: 'WARN', message: 'Example of ignored fields', timestamp: 'today' }); logger.debug({ level: 'WARN', msg: 'Example of ignored fields', timestamp: 'today' });
results in the following event logged as output to the console (note the results in the following event logged as output to the console (note the
ignored `level` and `timestamp` fields from the object): ignored `level` and `timestamp` fields from the object):
```json ```json
{"timestamp":"2021-09-01T00:00:00.000Z","level":"DEBUG","scope":"example","message":"Example of ignored fields"} {"timestamp":"2021-09-01T00:00:00.000Z","level":"DEBUG","scope":"example","msg":"Example of ignored fields"}
``` ```
This flattening behavior is implemented in the `flattenMessage` function This flattening behavior is implemented in the `flattenMessage` function

View File

@ -1,6 +1,6 @@
{ {
"name": "@jdbernard/logging", "name": "@jdbernard/logging",
"version": "2.0.0", "version": "2.1.0",
"description": "Simple Javascript logging service.", "description": "Simple Javascript logging service.",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
@ -8,7 +8,7 @@
"/dist" "/dist"
], ],
"scripts": { "scripts": {
"build": "tsc", "build": "bunx tsc",
"prepare": "npm run build", "prepare": "npm run build",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },

View File

@ -1,4 +1,4 @@
import { omit } from './util' import { omit } from "./util";
export enum LogLevel { export enum LogLevel {
ALL = 0, ALL = 0,
@ -25,7 +25,7 @@ export function parseLogLevel(
export interface LogMessage { export interface LogMessage {
scope: string; scope: string;
level: LogLevel; level: LogLevel;
message: string | Record<string, unknown>; msg: string | Record<string, unknown>;
stacktrace?: string; stacktrace?: string;
error?: Error; error?: Error;
timestamp: Date; timestamp: Date;
@ -53,32 +53,34 @@ export type FlattenedLogMessage = Record<string, unknown>;
* {"scope":"example","level":"INFO","foo":"bar","baz":"qux","timestamp":"2020-01-01T00:00:00.000Z"} * {"scope":"example","level":"INFO","foo":"bar","baz":"qux","timestamp":"2020-01-01T00:00:00.000Z"}
* ``` * ```
*/ */
export function flattenMessage(msg: LogMessage): FlattenedLogMessage { export function flattenMessage(logMsg: LogMessage): FlattenedLogMessage {
if (typeof msg.message === 'string') { if (typeof logMsg.msg === "string") {
return { ...msg, level: LogLevel[msg.level] }; return { ...logMsg, level: LogLevel[logMsg.level] };
} else { } else {
const { message, ...rest } = msg; const { msg, ...rest } = logMsg;
return { return {
...omit(message, [ ...omit(msg, [
'scope', "scope",
'level', "level",
'stacktrace', "stacktrace",
'error', "error",
'timestamp', "timestamp",
]), ]),
...rest, ...rest,
level: LogLevel[msg.level], level: LogLevel[logMsg.level],
}; };
} }
} }
export type LogMessageFormatter = (msg: LogMessage) => string | FlattenedLogMessage; export type LogMessageFormatter = (
msg: LogMessage,
) => string | FlattenedLogMessage;
export function structuredLogMessageFormatter(msg: LogMessage): string { export function structuredLogMessageFormatter(msg: LogMessage): string {
return JSON.stringify(flattenMessage(msg)); return JSON.stringify(flattenMessage(msg));
} }
export function simpleTextLogMessageFormatter(msg: LogMessage): string { export function simpleTextLogMessageFormatter(msg: LogMessage): string {
return `[${msg.scope}] - ${msg.level}: ${msg.message}`; return `[${msg.scope}] - ${msg.level}: ${msg.msg}`;
} }
export default LogMessage; export default LogMessage;

View File

@ -55,7 +55,7 @@ export class Logger {
public doLog( public doLog(
level: LogLevel, level: LogLevel,
message: Error | MessageType, msg: Error | MessageType,
stacktrace?: string, stacktrace?: string,
): void { ): void {
if (level < this.getEffectiveThreshold()) { if (level < this.getEffectiveThreshold()) {
@ -65,57 +65,57 @@ export class Logger {
const logMsg: LogMessage = { const logMsg: LogMessage = {
scope: this.name, scope: this.name,
level, level,
message: '', msg: '',
stacktrace: '', stacktrace: '',
timestamp: new Date(), timestamp: new Date(),
}; };
if (message === undefined || message === null) { if (msg === undefined || msg === null) {
logMsg.message = message; logMsg.msg = msg;
logMsg.stacktrace = stacktrace ?? ''; logMsg.stacktrace = stacktrace ?? '';
} else if (message instanceof Error) { } else if (msg instanceof Error) {
const error = message as Error; const error = msg as Error;
logMsg.error = error; logMsg.error = error;
logMsg.message = `${error.name}: ${error.message}`; logMsg.msg = `${error.name}: ${error.message}`;
logMsg.stacktrace = stacktrace ?? error.stack ?? ''; logMsg.stacktrace = stacktrace ?? error.stack ?? '';
} else if (isDeferredMsg(message)) { } else if (isDeferredMsg(msg)) {
logMsg.message = message(); logMsg.msg = msg();
logMsg.stacktrace = stacktrace == null ? '' : stacktrace; logMsg.stacktrace = stacktrace == null ? '' : stacktrace;
} else { } else {
// string | object // string | object
logMsg.message = message; logMsg.msg = msg;
logMsg.stacktrace = stacktrace == null ? '' : stacktrace; logMsg.stacktrace = stacktrace == null ? '' : stacktrace;
} }
this.sendToAppenders(logMsg); this.sendToAppenders(logMsg);
} }
public trace(message: Error | MessageType, stacktrace?: string): void { public trace(msg: Error | MessageType, stacktrace?: string): void {
this.doLog(LogLevel.TRACE, message, stacktrace); this.doLog(LogLevel.TRACE, msg, stacktrace);
} }
public debug(message: Error | MessageType, stacktrace?: string): void { public debug(msg: Error | MessageType, stacktrace?: string): void {
this.doLog(LogLevel.DEBUG, message, stacktrace); this.doLog(LogLevel.DEBUG, msg, stacktrace);
} }
public log(message: MessageType, stacktrace?: string): void { public log(msg: MessageType, stacktrace?: string): void {
this.doLog(LogLevel.LOG, message, stacktrace); this.doLog(LogLevel.LOG, msg, stacktrace);
} }
public info(message: MessageType, stacktrace?: string): void { public info(msg: MessageType, stacktrace?: string): void {
this.doLog(LogLevel.INFO, message, stacktrace); this.doLog(LogLevel.INFO, msg, stacktrace);
} }
public warn(message: MessageType, stacktrace?: string): void { public warn(msg: MessageType, stacktrace?: string): void {
this.doLog(LogLevel.WARN, message, stacktrace); this.doLog(LogLevel.WARN, msg, stacktrace);
} }
public error(message: Error | MessageType, stacktrace?: string): void { public error(msg: Error | MessageType, stacktrace?: string): void {
this.doLog(LogLevel.ERROR, message, stacktrace); this.doLog(LogLevel.ERROR, msg, stacktrace);
} }
public fatal(message: Error | MessageType, stacktrace?: string): void { public fatal(msg: Error | MessageType, stacktrace?: string): void {
this.doLog(LogLevel.FATAL, message, stacktrace); this.doLog(LogLevel.FATAL, msg, stacktrace);
} }
protected sendToAppenders(logMsg: LogMessage) { protected sendToAppenders(logMsg: LogMessage) {