Skip to content

Commit

Permalink
Merge pull request #84 from trojs/feature/console-format-json
Browse files Browse the repository at this point in the history
Possible to choose between console simple or json
  • Loading branch information
w3nl authored Aug 26, 2024
2 parents cecd4b8 + f27be92 commit eaf289d
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 37 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@ More info:

* level (default: trace)
* debug (default: false, stacktrace in console)
* format (default: simple, also possible to set to json which is useful for different log systems)
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@trojs/logger",
"description": "Winston logger for TroJS",
"version": "0.4.5",
"version": "0.5.0",
"author": {
"name": "Pieter Wigboldus",
"url": "https://trojs.org/"
Expand Down
8 changes: 7 additions & 1 deletion src/logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import winston from 'winston';
import makeLoggers from './loggers/index.js';

/**
* @typedef {import('./models/schemas/logger.js').Logger} LoggerType
* @typedef {import('./models/enums/level.js').LevelType} LevelType
*/

/** @type {LoggerType[]} */
const defaultLoggers = [
{
type: 'console',
Expand All @@ -19,7 +25,7 @@ const levels = {
/**
* Create the logger
* @param {object} config
* @param {Array=} config.loggers
* @param {LoggerType=} config.loggers
* @param {string?=} config.level
* @param {object?=} config.meta
* @returns {winston.Logger}
Expand Down
27 changes: 26 additions & 1 deletion src/loggers/__tests__/console.unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import makeConsoleLogger from '../console.js';

test('Test the console logger', async (t) => {
await t.test('It should make the console logger', () => {
const logger = makeConsoleLogger({ winston });
const logger = makeConsoleLogger({
winston,
logger: { type: 'console' },
});
assert.strictEqual(logger.name, 'console');
assert.strictEqual(logger instanceof winston.Transport, true);
assert.strictEqual(logger.level, 'trace');
Expand All @@ -18,4 +21,26 @@ test('Test the console logger', async (t) => {
assert.strictEqual(logger.filename, undefined);
assert.strictEqual(logger.options, undefined);
});
await t.test('It should make the json console logger', () => {
const logger = makeConsoleLogger({
winston,
logger: {
type: 'console',
format: 'json',
debug: true,
level: 'info',
},
});
assert.strictEqual(logger.name, 'console');
assert.strictEqual(logger instanceof winston.Transport, true);
assert.strictEqual(logger.level, 'info');
assert.strictEqual(logger.handleExceptions, undefined);
assert.strictEqual(logger.handleRejections, undefined);
assert.strictEqual(logger.silent, undefined);
assert.deepEqual(logger.stderrLevels, {});
assert.strictEqual(logger.writable, true);
assert.deepEqual(logger.consoleWarnLevels, {});
assert.strictEqual(logger.filename, undefined);
assert.strictEqual(logger.options, undefined);
});
});
17 changes: 10 additions & 7 deletions src/loggers/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ export default ({ winston, logger }) => {

return new winston.transports.Console({
level: logger?.level || defaultLevel,
format: winston.format.combine(
winston.format.errors({ stack: logger?.debug ?? false }),
winston.format(
stackDriver({ level: logger?.level, defaultLevel })
)(),
winston.format.json()
),
format:
logger.format === 'json'
? winston.format.combine(
winston.format.errors({ stack: logger?.debug ?? false }),
winston.format(
stackDriver({ level: logger?.level, defaultLevel })
)(),
winston.format.json()
)
: undefined,
});
};
12 changes: 12 additions & 0 deletions src/loggers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ const winstonLoggers = {
console: makeConsoleLogger,
};

/**
* @typedef {import('../models/schemas/logger.js').Logger} LoggerType
* @typedef {import('winston-transport').TransportStreamOptions} TransportStream
*/

/**
* Create all loggers
* @param {object} opts
* @param {object} opts.winston
* @param {LoggerType[]} opts.loggers
* @returns {TransportStream[]}
*/
const makeLoggers = ({ winston, loggers }) =>
Logger.createAll(loggers).map((logger) => {
const loggerFn = winstonLoggers[logger.type];
Expand Down
3 changes: 0 additions & 3 deletions src/models/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import LoggerMutator from './mutators/logger.js';
* @typedef {import('./schemas/logger.js').Logger} LoggerType
*/

/**
* @type {LoggerType}
*/
const LoggerObject = Obj({ schema: loggerSchema });

export default class Logger {
Expand Down
22 changes: 12 additions & 10 deletions src/models/schemas/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import Level from '../enums/level.js';
* The logger object
* @typedef {object} Logger
* @property {string} type
* @property {string?} location
* @property {LevelType?} level
* @property {string?} key
* @property {object?} credentials
* @property {string?} environment
* @property {string?} serverName
* @property {string?} release
* @property {boolean?} debug
* @property {number?} sampleRate
* @property {number?} tracesSampleRate
* @property {string=} location
* @property {LevelType=} level
* @property {string=} key
* @property {object=} credentials
* @property {string=} environment
* @property {string=} serverName
* @property {string=} release
* @property {boolean=} debug
* @property {number=} sampleRate
* @property {number=} tracesSampleRate
* @property {string=} format
*/

export default {
Expand All @@ -32,4 +33,5 @@ export default {
'debug?': Boolean,
'sampleRate?': Number,
'tracesSampleRate?': Number,
'format?': String,
};

0 comments on commit eaf289d

Please sign in to comment.