Improve Logging (#1519)

Improve Logging

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2021-09-28 22:06:35 +02:00 committed by GitHub
parent 1172a1d7b8
commit 0e512531a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 361 additions and 92 deletions

71
src/utils/logger.test.ts Normal file
View file

@ -0,0 +1,71 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Logger, LogLevel } from './logger'
import { Settings } from 'luxon'
describe('Logger', () => {
let consoleMock: jest.SpyInstance
let originalNow: () => number
let dateShift = 0
function mockConsole(methodToMock: LogLevel, onResult: (result: string) => void) {
consoleMock = jest.spyOn(console, methodToMock).mockImplementation((...data: string[]) => {
const result = data.reduce((state, current) => state + ' ' + current)
onResult(result)
})
}
beforeEach(() => {
originalNow = Settings.now
Settings.now = () => new Date(2021, 9, 25, dateShift, 1 + dateShift, 2 + dateShift, 3 + dateShift).valueOf()
})
afterEach(() => {
Settings.now = originalNow
consoleMock.mockReset()
})
it('logs a debug message into the console', (done) => {
dateShift = 0
mockConsole(LogLevel.DEBUG, (result) => {
expect(consoleMock).toBeCalled()
expect(result).toEqual('%c[2021-10-25 00:01:02] %c(prefix) color: yellow color: orange beans')
done()
})
new Logger('prefix').debug('beans')
})
it('logs a info message into the console', (done) => {
dateShift = 1
mockConsole(LogLevel.INFO, (result) => {
expect(consoleMock).toBeCalled()
expect(result).toEqual('%c[2021-10-25 01:02:03] %c(prefix) color: yellow color: orange toast')
done()
})
new Logger('prefix').info('toast')
})
it('logs a warn message into the console', (done) => {
dateShift = 2
mockConsole(LogLevel.WARN, (result) => {
expect(consoleMock).toBeCalled()
expect(result).toEqual('%c[2021-10-25 02:03:04] %c(prefix) color: yellow color: orange eggs')
done()
})
new Logger('prefix').warn('eggs')
})
it('logs a error message into the console', (done) => {
dateShift = 3
mockConsole(LogLevel.ERROR, (result) => {
expect(consoleMock).toBeCalled()
expect(result).toEqual('%c[2021-10-25 03:04:05] %c(prefix) color: yellow color: orange bacon')
done()
})
new Logger('prefix').error('bacon')
})
})

83
src/utils/logger.ts Normal file
View file

@ -0,0 +1,83 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { DateTime } from 'luxon'
export enum LogLevel {
DEBUG = 'debug',
INFO = 'info',
WARN = 'warn',
ERROR = 'error'
}
type OutputFunction = (...data: unknown[]) => void
/**
* Simple logger that prefixes messages with a timestamp and a name
*/
export class Logger {
private readonly scope: string
constructor(scope: string) {
this.scope = scope
}
/**
* Logs a debug message
* @param data data to log
*/
debug(...data: unknown[]): void {
this.log(LogLevel.DEBUG, ...data)
}
/**
* Logs a normal informative message
* @param data data to log
*/
info(...data: unknown[]): void {
this.log(LogLevel.INFO, ...data)
}
/**
* Logs a warning
* @param data data to log
*/
warn(...data: unknown[]): void {
this.log(LogLevel.WARN, ...data)
}
/**
* Logs an error
* @param data data to log
*/
error(...data: unknown[]): void {
this.log(LogLevel.ERROR, ...data)
}
private log(loglevel: LogLevel, ...data: unknown[]) {
const preparedData = [...this.prefix(), ...data]
const logOutput = Logger.getLogOutput(loglevel)
logOutput(...preparedData)
}
private static getLogOutput(logLevel: LogLevel): OutputFunction {
switch (logLevel) {
case LogLevel.INFO:
return console.info
case LogLevel.DEBUG:
return console.debug
case LogLevel.ERROR:
return console.error
case LogLevel.WARN:
return console.warn
}
}
private prefix(): string[] {
const timestamp = DateTime.now().toFormat('yyyy-MM-dd HH:mm:ss')
return [`%c[${timestamp}] %c(${this.scope})`, 'color: yellow', 'color: orange']
}
}