mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-14 07:04:45 -04:00
fix: Move content into to frontend directory
Doing this BEFORE the merge prevents a lot of merge conflicts. Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
4e18ce38f3
commit
762a0a850e
1051 changed files with 0 additions and 35 deletions
87
frontend/src/utils/logger.ts
Normal file
87
frontend/src/utils/logger.ts
Normal file
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 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']
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue