Last active
December 15, 2022 03:45
-
-
Save WorldMaker/25c4cbef665232e93b503e9db0ed33b8 to your computer and use it in GitHub Desktop.
Tagged Template Logger Wrapper for tslog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect } from 'chai' | |
import { Logger } from 'tslog' | |
import { TemplateLogger } from './template-logger' | |
describe('Template Logger', () => { | |
const logs: any[] = [] | |
const baseLogger = new Logger({ type: 'hidden' }) | |
baseLogger.attachTransport((logObj) => logs.push(logObj)) | |
afterEach(function () { | |
// clear logs | |
logs.length = 0 | |
}) | |
it('can log a debug template', () => { | |
const a = 45 | |
const b = true | |
const logger = new TemplateLogger(baseLogger) | |
logger.debug`Test that a = ${a} and b = ${b}` | |
expect(logs.length).to.equal(1) | |
expect(logs[0]._meta.logLevelName).to.equal('DEBUG') | |
expect(logs[0][0]).to.equal('Test that a = ') | |
expect(logs[0][1]).to.equal(a) | |
expect(logs[0][2]).to.equal(' and b = ') | |
expect(logs[0][3]).to.equal(b) | |
}) | |
it('can log an error template', () => { | |
const a = 33 | |
const b = new Error('Example') | |
const logger = new TemplateLogger(baseLogger) | |
logger.error`There was an error on the way to Club ${a} and it was ${b}` | |
expect(logs.length).to.equal(1) | |
expect(logs[0]._meta.logLevelName).to.equal('ERROR') | |
expect(logs[0][0]).to.equal('There was an error on the way to Club ') | |
expect(logs[0][1]).to.equal(a) | |
expect(logs[0][2]).to.equal(' and it was ') | |
expect(logs[0][3].nativeError).to.equal(b) | |
}) | |
it('can log an fatal template', () => { | |
const a = 13 | |
const b = new Error('OH NO!') | |
const logger = new TemplateLogger(baseLogger) | |
logger.error`${a} grim things happened and then ${b}` | |
expect(logs.length).to.equal(1) | |
expect(logs[0]._meta.logLevelName).to.equal('ERROR') | |
expect(logs[0][0]).to.equal('') | |
expect(logs[0][1]).to.equal(a) | |
expect(logs[0][2]).to.equal(' grim things happened and then ') | |
expect(logs[0][3].nativeError).to.equal(b) | |
}) | |
it('can log an info template', () => { | |
const a = 97 | |
const b = ['hello'] | |
const logger = new TemplateLogger(baseLogger) | |
logger.info`But did you know that a = ${a} and b = ${b}?` | |
expect(logs.length).to.equal(1) | |
expect(logs[0]._meta.logLevelName).to.equal('INFO') | |
expect(logs[0][0]).to.equal('But did you know that a = ') | |
expect(logs[0][1]).to.equal(a) | |
expect(logs[0][2]).to.equal(' and b = ') | |
expect(logs[0][3]).to.deep.equal(b) | |
expect(logs[0][4]).to.equal('?') | |
}) | |
it('can log a silly template', () => { | |
const a = 420 | |
const b = { hello: 'world' } | |
const logger = new TemplateLogger(baseLogger) | |
logger.silly`Hanging out ${a} and ${b}` | |
expect(logs.length).to.equal(1) | |
expect(logs[0]._meta.logLevelName).to.equal('SILLY') | |
expect(logs[0][0]).to.equal('Hanging out ') | |
expect(logs[0][1]).to.equal(a) | |
expect(logs[0][2]).to.equal(' and ') | |
expect(logs[0][3]).to.deep.equal(b) | |
}) | |
it('can log a trace template', () => { | |
const a = false | |
const b = { good: 'morrow' } | |
const logger = new TemplateLogger(baseLogger) | |
logger.trace`${b}: ${a}` | |
expect(logs.length).to.equal(1) | |
expect(logs[0]._meta.logLevelName).to.equal('TRACE') | |
expect(logs[0].length === 4) | |
expect(logs[0][0]).to.equal('') | |
expect(logs[0][1]).to.deep.equal(b) | |
expect(logs[0][2]).to.equal(': ') | |
expect(logs[0][3]).to.equal(a) | |
}) | |
it('can log a warn template', () => { | |
const a = ['nope'] | |
const b = new Error('Something to watch for') | |
const logger = new TemplateLogger(baseLogger) | |
logger.warn`Seeing ${a} and ${b}` | |
expect(logs.length).to.equal(1) | |
expect(logs[0]._meta.logLevelName).to.equal('WARN') | |
expect(logs[0][0]).to.equal('Seeing ') | |
expect(logs[0][1]).to.deep.equal(a) | |
expect(logs[0][2]).to.equal(' and ') | |
expect(logs[0][3].nativeError).to.equal(b) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Logger } from 'tslog' | |
function* interleaveArgs(strings: TemplateStringsArray, ...values: unknown[]) { | |
for (let i = 0; i < strings.length; i++) { | |
yield strings[i] | |
if (i < values.length) { | |
yield values[i] | |
} | |
} | |
} | |
export class TemplateLogger { | |
constructor(protected logger: Logger<any>) {} | |
debug(strings: TemplateStringsArray, ...values: unknown[]) { | |
return this.logger.debug(...interleaveArgs(strings, ...values)) | |
} | |
error(strings: TemplateStringsArray, ...values: unknown[]) { | |
return this.logger.error(...interleaveArgs(strings, ...values)) | |
} | |
fatal(strings: TemplateStringsArray, ...values: unknown[]) { | |
return this.logger.fatal(...interleaveArgs(strings, ...values)) | |
} | |
info(strings: TemplateStringsArray, ...values: unknown[]) { | |
return this.logger.info(...interleaveArgs(strings, ...values)) | |
} | |
silly(strings: TemplateStringsArray, ...values: unknown[]) { | |
return this.logger.silly(...interleaveArgs(strings, ...values)) | |
} | |
trace(strings: TemplateStringsArray, ...values: unknown[]) { | |
return this.logger.trace(...interleaveArgs(strings, ...values)) | |
} | |
warn(strings: TemplateStringsArray, ...values: unknown[]) { | |
return this.logger.warn(...interleaveArgs(strings, ...values)) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Logger } from 'tslog' | |
import { TemplateLogger } from './logging/template-logger' | |
const logger = new TemplateLogger(new Logger()) | |
{ | |
const a = 45 | |
const b = true | |
logger.debug`Test that a = ${a} and b = ${b}` | |
} | |
{ | |
const a = 33 | |
const b = new Error('Example') | |
logger.error`There was an error on the way to Club ${a} and it was ${b}` | |
} | |
{ | |
const a = 13 | |
const b = new Error('OH NO!') | |
logger.error`${a} grim things happened and then ${b}` | |
} | |
{ | |
const a = 97 | |
const b = ['hello'] | |
logger.info`But did you know that a = ${a} and b = ${b}?` | |
} | |
{ | |
const a = 420 | |
const b = { hello: 'world' } | |
logger.silly`Hanging out ${a} and ${b}` | |
} | |
{ | |
const a = false | |
const b = { good: 'morrow' } | |
logger.trace`${b}: ${a}` | |
} | |
{ | |
const a = ['nope'] | |
const b = new Error('Something to watch for') | |
logger.warn`Seeing ${a} and ${b}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment