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 */ public abstract write(message: LogMessage): Awaitable; /** * 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') } } }