Standard libraries that lift up your code.
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.
 
 
 

51 lines
1.6 KiB

import {LoggingLevel, LogMessage} from './types'
import * as color from 'colors/safe'
import {Awaitable} from '../support/types'
/**
* Base class for an application logger.
*/
export abstract class Logger {
/**
* Write the given message to the log destination.
* @param {LogMessage} message
* @return Promise<void>
*/
public abstract write(message: LogMessage): Awaitable<void>;
/**
* Format the date object to the string output format.
* @param {Date} date
* @return string
*/
protected formatDate(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 levelDisplay(level: LoggingLevel): string {
switch (level) {
case LoggingLevel.Success:
return color.green('success')
case LoggingLevel.Error:
return color.red(' error')
case LoggingLevel.Warning:
return color.yellow('warning')
case LoggingLevel.Info:
return color.blue(' info')
case LoggingLevel.Debug:
return color.cyan(' debug')
case LoggingLevel.Verbose:
return color.gray('verbose')
case LoggingLevel.Silent:
return color.gray(' silent')
}
}
}