2 Commits
2.1.0 ... 2.2.0

Author SHA1 Message Date
f33ca24b53 timestamp -> ts; error -> err 2025-07-14 15:38:40 -05:00
318135e82b Update README based on naming convention change. 2025-06-26 10:58:27 -05:00
5 changed files with 25 additions and 25 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.
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
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.
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
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:
```typescript
@ -87,24 +87,24 @@ 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":"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"}
```
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
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:
```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
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"}
{"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

View File

@ -1,6 +1,6 @@
{
"name": "@jdbernard/logging",
"version": "2.1.0",
"version": "2.2.0",
"description": "Simple Javascript logging service.",
"main": "dist/index.js",
"types": "dist/index.d.ts",

View File

@ -60,19 +60,19 @@ export class ConsoleLogAppender implements LogAppender {
const fmtMsg = this.formatter(msg);
if (typeof fmtMsg === "string") {
if (msg.error || msg.stacktrace) {
logMethod(fmtMsg, msg.error ?? msg.stacktrace);
if (msg.err || msg.stacktrace) {
logMethod(fmtMsg, msg.err ?? msg.stacktrace);
} else {
logMethod(fmtMsg);
}
} else {
const { message, _error, _stacktrace, ...rest } = fmtMsg;
const { message, _err, _stacktrace, ...rest } = fmtMsg;
const summary = `${LogLevel[msg.level]} -- ${msg.scope}: ${
message ?? fmtMsg.method
}\n`;
if (msg.error || msg.stacktrace) {
logMethod(summary, msg.error ?? msg.stacktrace, rest);
if (msg.err || msg.stacktrace) {
logMethod(summary, msg.err ?? msg.stacktrace, rest);
} else {
logMethod(summary, rest);
}

View File

@ -27,8 +27,8 @@ export interface LogMessage {
level: LogLevel;
msg: string | Record<string, unknown>;
stacktrace?: string;
error?: Error;
timestamp: Date;
err?: Error;
ts: Date;
}
export type FlattenedLogMessage = Record<string, unknown>;
@ -45,12 +45,12 @@ export type FlattenedLogMessage = Record<string, unknown>;
*
* ```typescript
* const logger = logService.getLogger('example');
* logger.info({ foo: 'bar', baz: 'qux', timestamp: 'today', level: LogLevel.WARN });
* logger.info({ foo: 'bar', baz: 'qux', ts: '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"}
* {"scope":"example","level":"INFO","foo":"bar","baz":"qux","ts":"2020-01-01T00:00:00.000Z"}
* ```
*/
export function flattenMessage(logMsg: LogMessage): FlattenedLogMessage {
@ -63,8 +63,8 @@ export function flattenMessage(logMsg: LogMessage): FlattenedLogMessage {
"scope",
"level",
"stacktrace",
"error",
"timestamp",
"err",
"ts",
]),
...rest,
level: LogLevel[logMsg.level],

View File

@ -67,17 +67,17 @@ export class Logger {
level,
msg: '',
stacktrace: '',
timestamp: new Date(),
ts: new Date(),
};
if (msg === undefined || msg === null) {
logMsg.msg = msg;
logMsg.stacktrace = stacktrace ?? '';
} else if (msg instanceof Error) {
const error = msg as Error;
logMsg.error = error;
logMsg.msg = `${error.name}: ${error.message}`;
logMsg.stacktrace = stacktrace ?? error.stack ?? '';
const err = msg as Error;
logMsg.err = err;
logMsg.msg = `${err.name}: ${err.message}`;
logMsg.stacktrace = stacktrace ?? err.stack ?? '';
} else if (isDeferredMsg(msg)) {
logMsg.msg = msg();
logMsg.stacktrace = stacktrace == null ? '' : stacktrace;