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.

78 lines
2.3 KiB

import { LogMessage, LoggingLevel } from './types.ts'
import {make} from '../../../../di/src/global.ts'
import {Logging} from './Logging.ts'
import {blue, cyan, gray, green, red, yellow} from '../../external/std.ts'
/**
* Returns true if the given item is a typeof the Logger class.
* @param something
* @return boolean
*/
const isLoggerClass = (something: any): something is (typeof Logger) => {
return something.prototype instanceof Logger
}
export { isLoggerClass }
/**
* Base class for an application logger.
*/
export default abstract class Logger {
/**
* Write the given message to the log destination.
* @param {LogMessage} message
* @return Promise<void>
*/
public abstract async write(message: LogMessage): Promise<void>;
/**
* Register this logger with the logging service.
*/
public static register() {
make(Logging).register_logger(this)
}
/**
* Remove this logger from the logging service.
*/
public static remove() {
make(Logging).remove_logger(this)
}
/**
* Format the date object to the string output format.
* @param {Date} date
* @return string
*/
protected format_date(date: Date): string {
const hours = date.getHours()
const minutes = date.getMinutes()
const seconds = date.getSeconds()
return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} ${hours > 9 ? hours : '0' + hours}:${minutes > 9 ? minutes : '0' + minutes}:${seconds > 9 ? seconds : '0' + seconds}`
}
/**
* Given a logging level, get the display string of that level.
* @param {LoggingLevel} level
* @return string
*/
protected level_display(level: LoggingLevel): string {
switch(level) {
case LoggingLevel.Success:
return green('success')
case LoggingLevel.Error:
return red(' error')
case LoggingLevel.Warning:
return yellow('warning')
case LoggingLevel.Info:
return blue(' info')
case LoggingLevel.Debug:
return cyan(' debug')
case LoggingLevel.Verbose:
return gray('verbose')
case LoggingLevel.Silent:
return gray(' silent')
}
}
}