Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/ProcLog.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { LogBase } from './LogBase.js'
import { Log } from './node.js'
import { inspectOpts, inspectNamespaces, INFO } from './utils.js'
import { wrapConsole } from './wrapConsole.js'
import { wrapDebug } from './wrapDebug.js'

/** @typedef {import('./utils.js').Level} Level */
/** @typedef {import('./node.js').LogOptions} LogOptions */
/** @typedef {import('./node.js').LogOptionWrapConsole} LogOptionWrapConsole */
/** @typedef {LogOptions & {Log: typeof Log}} LogOptionsWithCustomLog */

/**
* @typedef {object} ProcLogOptions
* @property {Level} [level] log level
Expand All @@ -13,7 +17,7 @@ import { inspectOpts, inspectNamespaces, INFO } from './utils.js'

export const EVENT_PROC_LOG = 'log-level'

const defaultOptions = {
const options = {
level: INFO,
namespaces: undefined
}
Expand Down Expand Up @@ -57,7 +61,7 @@ export class ProcLog extends LogBase {
*/
constructor(name, opts) {
const _opts = {
...defaultOptions,
...options,
...inspectOpts(process.env),
...inspectNamespaces(process.env),
...opts,
Expand All @@ -73,6 +77,24 @@ export class ProcLog extends LogBase {
// @ts-expect-error
process.emit(EVENT_PROC_LOG, level, this.name, fmt, args)
}

/**
* @param {string} [name]
* @param {ProcLogOptions & LogOptionWrapConsole} [opts]
* @returns {() => void} unwrap functions
*/
static wrapConsole(name = 'console', opts) {
const log = new ProcLog(name, opts)
return wrapConsole(log, opts)
}

/**
* @param {ProcLogOptions} [opts]
* @returns {() => void} unwrap functions
*/
static wrapDebug(opts) {
return wrapDebug(ProcLog, opts)
}
}

/**
Expand Down
30 changes: 23 additions & 7 deletions src/ecs/LogEcs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Log, stringify } from '../node.js'
import { ecsSerializers } from './serializers.js'
import { wrapConsole } from '../wrapConsole.js'
import { wrapDebug } from '../wrapDebug.js'

/**
* @typedef {import('../serializers/index.js').Serializer} Serializer
*/
/**
* @typedef {import('../node.js').LogOptions & {serializers: Record<string, Serializer>}} LogOptionsEcs
* @typedef {import('../node.js').LogOptionWrapConsole} LogOptionWrapConsole
*/
/** @typedef {import('../serializers/index.js').Serializer} Serializer */
/** @typedef {import('../node.js').LogOptions & {serializers: Record<string, Serializer>}} LogOptionsEcs */
/** @typedef {import('../node.js').LogOptionWrapConsole} LogOptionWrapConsole */

/**
* Elastic Common Schema (ECS) compatible logger;
Expand All @@ -33,6 +31,24 @@ export class LogEcs extends Log {
this.toJson = this._toJson
}

/**
* @param {string} [name]
* @param {LogOptionsEcs & LogOptionWrapConsole} [opts]
* @returns {() => void} unwrap function
*/
static wrapConsole(name = 'console', opts) {
const log = new LogEcs(name, opts)
return wrapConsole(log, opts)
}

/**
* @param {LogOptionsEcs} [opts]
* @returns {() => void} unwrap function
*/
static wrapDebug(opts) {
return wrapDebug(LogEcs, opts)
}

/* c8 ignore next 18 */
_applySerializers(obj) {
const ecsObj = {}
Expand Down
9 changes: 5 additions & 4 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class Log extends LogBase {
options.serializers,
opts ? opts.serializers : {}
)
const _opts = Object.assign({}, options, opts, { serializers })
const _opts = { ...options, ...opts, serializers }
super(name, _opts)

const colorFn = (n) => chalk.hex(n)
Expand Down Expand Up @@ -195,10 +195,11 @@ export class Log extends LogBase {
}

/**
* @returns {() => void} unwrap functions
* @param {LogOptions} [opts] - see Log.options
* @returns {() => void} unwrap function
*/
static wrapDebug() {
return wrapDebug(Log)
static wrapDebug(opts) {
return wrapDebug(Log, opts)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/wrapDebug.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const unwrap = () => {
}
}

export function wrapDebug(Log) {
export function wrapDebug(Log, opts) {
if (wrapped) return unwrap
class Loggers {
constructor() {
Expand All @@ -21,7 +21,7 @@ export function wrapDebug(Log) {
get(namespace) {
let logger = this.cache[namespace]
if (!logger) {
logger = this.cache[namespace] = new Log(namespace)
logger = this.cache[namespace] = new Log(namespace, opts)
}
return logger
}
Expand Down
47 changes: 47 additions & 0 deletions test/LogEcs.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'assert'
import os from 'os'
import sinon from 'sinon'
import debug from 'debug'
import { startTimeKey } from '../src/serializers/res.js'
import { LogEcs, ecsSerializers } from '../src/ecs/index.js'
import { httpLogs, logger } from '../src/index.js'
Expand Down Expand Up @@ -413,4 +414,50 @@ describe('LogEcs', function () {
)
})
})

describe('wrap console', function () {
let unwrap
before(function () {
unwrap = LogEcs.wrapConsole('test', {
level: 'trace',
namespaces: 'test'
})
})
after(function () {
unwrap()
})

it('shall wrap console.log', function () {
console.log('log %s', 'log')
console.trace('trace')
console.debug({ debug: true })
console.info('log %j', { info: 1 })
console.warn('warn')
console.error(new Error('Baam'))
})

it('shall not wrap console twice', function () {
const unwrap1 = LogEcs.wrapConsole('test1')
const unwrap2 = LogEcs.wrapConsole('test2')
assert.strictEqual(unwrap1, unwrap)
assert.strictEqual(unwrap2, unwrap)
})
})

describe('wrap debug', function () {
let unwrap
before(function () {
const options = { level: 'debug', namespaces: '*' }
unwrap = LogEcs.wrapDebug(options)
})
after(function () {
unwrap()
})

it('shall wrap debug', function () {
const log = debug('namespace')
log.enabled = '*'
log('hello %s', 'log')
})
})
})
Loading