You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.2 KiB
79 lines
2.2 KiB
import {LoggingLevel, LogMessage} from './types.ts'
|
|
import Logger from './Logger.ts'
|
|
import {Service} from '../../../../di/src/decorator/Service.ts'
|
|
import {make} from '../../../../di/src/global.ts'
|
|
import {isInstantiable} from '../../../../di/src/type/Instantiable.ts'
|
|
|
|
@Service()
|
|
class Logging {
|
|
private _level = LoggingLevel.Warning
|
|
private _loggers: Logger[] = []
|
|
|
|
public get level() {
|
|
return this._level
|
|
}
|
|
|
|
public set level(level) {
|
|
this._level = level
|
|
}
|
|
|
|
public success(output: any, force = false) {
|
|
this.write_log(LoggingLevel.Success, output, force)
|
|
}
|
|
|
|
public error(output: any, force = false) {
|
|
this.write_log(LoggingLevel.Error, output, force)
|
|
}
|
|
|
|
public warn(output: any, force = false) {
|
|
this.write_log(LoggingLevel.Warning, output, force)
|
|
}
|
|
|
|
public info(output: any, force = false) {
|
|
this.write_log(LoggingLevel.Info, output, force)
|
|
}
|
|
|
|
public debug(output: any, force = false) {
|
|
this.write_log(LoggingLevel.Debug, output, force)
|
|
}
|
|
|
|
public verbose(output: any, force = false) {
|
|
this.write_log(LoggingLevel.Verbose, output, force)
|
|
}
|
|
|
|
protected write_log(level: LoggingLevel, output: any, force = false) {
|
|
const message = this.build_message(level, output)
|
|
if ( this._level >= level || force ) {
|
|
for ( const logger of this._loggers ) {
|
|
try {
|
|
logger.write(message)
|
|
} catch (e) {
|
|
console.error('logging error', e)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
protected build_message(level: LoggingLevel, output: any): LogMessage {
|
|
return {
|
|
level,
|
|
output,
|
|
date: new Date,
|
|
}
|
|
}
|
|
|
|
public register_logger(logger_class: typeof Logger) {
|
|
if ( isInstantiable(logger_class) ) {
|
|
const logger = make(logger_class)
|
|
if ( !this._loggers.includes(logger) )
|
|
this._loggers.push(logger)
|
|
}
|
|
}
|
|
|
|
public remove_logger(logger_class: typeof Logger) {
|
|
this._loggers = this._loggers.filter(x => !(x instanceof logger_class))
|
|
}
|
|
}
|
|
|
|
export { Logging }
|