Skip to content

Commit 2e4de38

Browse files
committed
fix: Add missing trace level
* Add trace to level options/validation * Lower min level for streams to trace * add loggerTrace helper logger
1 parent 1468f99 commit 2e4de38

File tree

8 files changed

+38
-14
lines changed

8 files changed

+38
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ These are the loggers that should be used for the majority of your application.
7575

7676
These loggers are pre-defined for specific use cases:
7777

78+
* [`loggerTrace`](https://foxxmd.github.io/logging/variables/index.loggerTrace.html) - Logs to console at the lowest minimum level, `trace`. Can be used during application startup before a logger app configuration has been parsed.
7879
* [`loggerDebug`](https://foxxmd.github.io/logging/variables/index.loggerDebug.html) - Logs ONLY to console at minimum `debug` level. Can be used during application startup before a logger app configuration has been parsed.
7980
* [`loggerTest`](https://foxxmd.github.io/logging/variables/index.loggerTest.html) - A noop logger (will not log anywhere) for use in tests/mockups.
8081

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@foxxmd/logging",
33
"type": "module",
4-
"version": "0.2.5",
4+
"version": "0.2.6",
55
"repository": "https://github.com/foxxmd/logging",
66
"description": "A typed, opinionated, batteries-included, Pino-based logging solution for backend TS/JS projects",
77
"scripts": {

src/funcs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const isFileLogOptions = (obj: any): obj is FileLogOptions => {
4646
* */
4747
export const parseLogOptions = (config: LogOptions = {}, options?: FileLogPathOptions): LogOptionsParsed => {
4848
if (!isLogOptions(config)) {
49-
throw new Error(`Logging levels were not valid. Must be one of: 'silent', 'fatal', 'error', 'warn', 'info', 'verbose', 'debug', -- 'file' may be false.`)
49+
throw new Error(`Logging levels were not valid. Must be one of: 'silent', 'fatal', 'error', 'warn', 'info', 'verbose', 'debug', 'trace' -- 'file' may be false.`)
5050
}
5151

5252
const {level: configLevel} = config;

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
parseLogOptions,
1717
} from './funcs.js'
1818

19-
import {childLogger, loggerApp, loggerDebug, loggerTest, loggerAppRolling} from './loggers.js';
19+
import {childLogger, loggerApp, loggerDebug, loggerTest, loggerTrace, loggerAppRolling} from './loggers.js';
2020

2121
export type {
2222
FileLogOptions,
@@ -35,6 +35,7 @@ export {
3535
loggerAppRolling,
3636
loggerDebug,
3737
loggerTest,
38+
loggerTrace,
3839
LOG_LEVEL_NAMES,
3940
childLogger,
4041
parseLogOptions,

src/loggers.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ import {pino, levels, stdSerializers} from "pino";
2020
* */
2121
export const buildLogger = (defaultLevel: LogLevel, streams: LogLevelStreamEntry[], extras: PinoLoggerOptions = {}): Logger => {
2222
// TODO maybe implement custom levels
23-
//const { levels = {} } = extras;
23+
//const { levels: extraLevels = {} } = extras;
24+
/*
25+
pino levels are found at pino/lib/constants.js
26+
trace: 10,
27+
debug: 20,
28+
info: 30,
29+
warn: 40,
30+
error: 50,
31+
fatal: 60
32+
33+
MS custom levels
34+
verbose: 25
35+
log: 21
36+
*/
37+
const ms = pino.multistream(streams, {levels: {...levels.values, ...CUSTOM_LEVELS}});
2438

2539
const plogger = pino<"verbose" | "log">({
2640
// @ts-ignore
@@ -59,7 +73,7 @@ export const buildLogger = (defaultLevel: LogLevel, streams: LogLevelStreamEntry
5973
},
6074
},
6175
...extras
62-
}, pino.multistream(streams, {levels: {...levels.values, ...CUSTOM_LEVELS}})) as Logger;
76+
}, ms) as Logger;
6377
plogger.labels = [];
6478

6579
plogger.addLabel = function (value) {
@@ -96,15 +110,22 @@ export const childLogger = (parent: Logger, labelsVal: any | any[] = [], context
96110
*
97111
* @source
98112
* */
99-
export const loggerTest = buildLogger('silent', [buildDestinationStdout('debug')]);
113+
export const loggerTest = buildLogger('silent', [buildDestinationStdout('trace')]);
100114

101115

102116
/**
103117
* A logger that ONLY logs to console at minimum 'debug' level. Useful for logging during startup before a loggerApp has been initialized
104118
*
105119
* @source
106120
* */
107-
export const loggerDebug = buildLogger('debug', [buildDestinationStdout('debug')]);
121+
export const loggerDebug = buildLogger('debug', [buildDestinationStdout('trace')]);
122+
123+
/**
124+
* A logger that logs to console at the lowest minimum level, 'trace'. Useful for logging during startup before a loggerApp has been initialized
125+
*
126+
* @source
127+
* */
128+
export const loggerTrace = buildLogger('trace', [buildDestinationStdout('trace')]);
108129

109130
/**
110131
* A Logger that logs to console and a static file
@@ -135,7 +156,7 @@ export const loggerApp = (config: LogOptions | object = {}, extras?: LoggerAppEx
135156
}
136157
}
137158

138-
const logger = buildLogger('debug' as LogLevel, streams, pino);
159+
const logger = buildLogger('trace' as LogLevel, streams, pino);
139160
if (error !== undefined) {
140161
logger.warn(error);
141162
}
@@ -170,7 +191,7 @@ export const loggerAppRolling = async (config: LogOptions | object = {}, extras?
170191
error = e;
171192
}
172193
}
173-
const logger = buildLogger('debug' as LogLevel, streams, pino);
194+
const logger = buildLogger('trace' as LogLevel, streams, pino);
174195
if (error !== undefined) {
175196
logger.warn(error);
176197
}

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {MarkRequired} from "ts-essentials";
77
*
88
* From lowest to highest:
99
*
10+
* * `trace`
1011
* * `debug`
1112
* * `verbose`
1213
* * `log`
@@ -48,7 +49,7 @@ export const CUSTOM_LEVELS: LoggerOptions<"verbose" | "log">['customLevels'] = {
4849

4950
const CUSTOM_LEVEL_NAMES = Object.keys(CUSTOM_LEVELS);
5051

51-
export const LOG_LEVEL_NAMES= ['silent', 'fatal', 'error', 'warn', 'info', 'log', 'verbose', 'debug'] as const;
52+
export const LOG_LEVEL_NAMES= ['silent', 'fatal', 'error', 'warn', 'info', 'log', 'verbose', 'debug', 'trace'] as const;
5253

5354
/**
5455
* Configure log levels and file options for an AppLogger.

tests/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const testConsoleLogger = (config?: object, colorize = false): [Logger, Transfor
3232
const opts = parseLogOptions(config, {logBaseDir: process.cwd()});
3333
const testStream = new PassThrough();
3434
const rawStream = new PassThrough();
35-
const logger = buildLogger('debug', [
35+
const logger = buildLogger('trace', [
3636
buildDestinationStream(
3737
opts.console,
3838
{
@@ -53,7 +53,7 @@ const testObjectLogger = (config?: object, object?: boolean): [Logger, Transform
5353
const opts = parseLogOptions(config, {logBaseDir: process.cwd()});
5454
const testStream = new PassThrough({objectMode: true});
5555
const rawStream = new PassThrough();
56-
const logger = buildLogger('debug', [
56+
const logger = buildLogger('trace', [
5757
buildDestinationJsonPrettyStream(
5858
opts.console,
5959
{

0 commit comments

Comments
 (0)