@@ -0,0 +1,14 @@
|
||||
import {Chalk} from 'chalk'
|
||||
import {Level} from 'types'
|
||||
|
||||
const chalk = new Chalk({ level: 2 }) // 256 colors
|
||||
|
||||
export function get_color(level: Level) {
|
||||
switch (level) {
|
||||
case Level.DEBUG: return chalk.blueBright
|
||||
case Level.TRACE: return chalk.green
|
||||
case Level.INFO: return (str: string) => str
|
||||
case Level.WARNING: return chalk.hex('#FFA500')
|
||||
case Level.ERROR: return chalk.red
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,11 @@
|
||||
import {Level, level_to_string, type Options} from 'types'
|
||||
import {Writer} from 'writer'
|
||||
import {Level, type Options, type Writer} from 'types'
|
||||
import {get_color} from 'color'
|
||||
|
||||
export const writers = new Map<string, Writer>
|
||||
export let options: Options = {
|
||||
format: "[$time] $level $namespace :",
|
||||
}
|
||||
|
||||
export function set_options(opt: Partial<Options>) {
|
||||
options = {...options, ...opt}
|
||||
}
|
||||
export function get_options() : Readonly<Options> {
|
||||
return options
|
||||
}
|
||||
|
||||
export function set_writer(name: string, writer: Writer) {
|
||||
writers.set(name, writer)
|
||||
}
|
||||
export function get_writer(name: string) : Readonly<Writer> | undefined {
|
||||
return writers.get(name)
|
||||
}
|
||||
export function remove_writer(name: string) : boolean {
|
||||
return writers.delete(name)
|
||||
}
|
||||
|
||||
export class Logger {
|
||||
private readonly _namespace: string
|
||||
|
||||
@@ -73,3 +56,22 @@ function log(message: any[], level: Level, namespace: string) : void {
|
||||
writer.log(level, writer.options.with_color ? head_color : head_bw, ...message)
|
||||
}
|
||||
}
|
||||
|
||||
function level_to_string(level: Level, with_color: boolean) : string {
|
||||
const str = get_string(level)
|
||||
|
||||
if (!with_color) return str
|
||||
|
||||
const color = get_color(level)
|
||||
return color(str)
|
||||
}
|
||||
|
||||
function get_string(level: Level) : string {
|
||||
switch (level) {
|
||||
case Level.DEBUG: return "DEBUG "
|
||||
case Level.TRACE: return "TRACE "
|
||||
case Level.INFO: return "INFO "
|
||||
case Level.WARNING: return "WARNING"
|
||||
case Level.ERROR: return "ERROR "
|
||||
}
|
||||
}
|
||||
+12
-28
@@ -1,5 +1,3 @@
|
||||
import { Chalk } from 'chalk'
|
||||
|
||||
export type Options = {
|
||||
format: string,
|
||||
}
|
||||
@@ -27,33 +25,19 @@ export type WriterOptions = {
|
||||
[key: string | number | symbol]: any,
|
||||
}
|
||||
|
||||
export function level_to_string(level: Level, with_color: boolean) : string {
|
||||
const str = get_string(level)
|
||||
export abstract class Writer {
|
||||
protected readonly _options: WriterOptions;
|
||||
protected constructor(options: WriterOptions) {
|
||||
this._options = options
|
||||
}
|
||||
|
||||
if (!with_color) return str
|
||||
public log(level: Level, ...data: any[]) : void {
|
||||
if (level < this.options.minLevel) return
|
||||
this.write(level, ...data)
|
||||
}
|
||||
protected abstract write(level: Level, ...data: any[]) : void;
|
||||
|
||||
const color = get_color(level)
|
||||
return color(str)
|
||||
}
|
||||
|
||||
function get_string(level: Level) : string {
|
||||
switch (level) {
|
||||
case Level.DEBUG: return "DEBUG "
|
||||
case Level.TRACE: return "TRACE "
|
||||
case Level.INFO: return "INFO "
|
||||
case Level.WARNING: return "WARNING"
|
||||
case Level.ERROR: return "ERROR "
|
||||
}
|
||||
}
|
||||
|
||||
const chalk = new Chalk({ level: 2 }) // 256 colors
|
||||
|
||||
function get_color(level: Level) {
|
||||
switch (level) {
|
||||
case Level.DEBUG: return chalk.blueBright
|
||||
case Level.TRACE: return chalk.green
|
||||
case Level.INFO: return (str: string) => str
|
||||
case Level.WARNING: return chalk.hex('#FFA500')
|
||||
case Level.ERROR: return chalk.red
|
||||
public get options() : WriterOptions {
|
||||
return this._options
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import {Writer} from 'writer'
|
||||
import {Level} from 'types'
|
||||
|
||||
export class Console extends Writer {
|
||||
protected write(level: Level, ...data: any[]) : void {
|
||||
Console.get_logger(level)(...data)
|
||||
}
|
||||
|
||||
private static get_logger(level: Level) {
|
||||
switch (level) {
|
||||
case Level.DEBUG: return console.debug
|
||||
case Level.TRACE: return console.debug
|
||||
case Level.INFO: return console.log
|
||||
case Level.WARNING: return console.warn
|
||||
case Level.ERROR: return console.error
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import {Writer} from 'writer'
|
||||
import {Level} from 'types'
|
||||
|
||||
export class Counter extends Writer {
|
||||
private readonly calls = {
|
||||
debug: 0,
|
||||
trace: 0,
|
||||
info: 0,
|
||||
warn: 0,
|
||||
error: 0,
|
||||
}
|
||||
|
||||
public constructor() {
|
||||
super({minLevel: Level.DEBUG, with_color: false});
|
||||
}
|
||||
|
||||
protected write(level: Level, ...data: any[]): void {
|
||||
switch (level) {
|
||||
case Level.DEBUG: this.calls.debug++; break
|
||||
case Level.TRACE: this.calls.trace++; break
|
||||
case Level.INFO: this.calls.info++; break
|
||||
case Level.WARNING: this.calls.warn++; break
|
||||
case Level.ERROR: this.calls.error++; break
|
||||
}
|
||||
}
|
||||
|
||||
public get count() {
|
||||
return this.calls
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
import {Writer} from 'writer'
|
||||
import {type WriterOptions, Level} from 'types'
|
||||
|
||||
export class File extends Writer {
|
||||
public constructor(options: WriterOptions & { path: string }) {
|
||||
const {path, ...rest} = options
|
||||
super(rest)
|
||||
}
|
||||
|
||||
protected write(level: Level, ...data: any[]) : void {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
|
||||
export class DailyFile extends Writer {
|
||||
private _writer: File
|
||||
private readonly _folder: string
|
||||
private now: Date = new Date()
|
||||
|
||||
public constructor(options: WriterOptions & {folder: string}) {
|
||||
const {folder, ...rest} = options
|
||||
|
||||
super(rest)
|
||||
this._folder = folder
|
||||
|
||||
this._writer = DailyFile.start(this.options, this._folder, this.now)
|
||||
}
|
||||
|
||||
private static start(options: WriterOptions, folder: string, now: Date) : File {
|
||||
const date = now.toISOString().split('T')[0]
|
||||
const path = `${folder}/${date}.log`
|
||||
return new File({...options, path})
|
||||
}
|
||||
|
||||
protected write(level: Level, ...data: any[]): void {
|
||||
if (new Date().getDay() !== this.now.getDay()) {
|
||||
this.now = new Date()
|
||||
this._writer = DailyFile.start(this.options, this._folder, this.now)
|
||||
}
|
||||
|
||||
return this._writer.log(level, data)
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
export {Console} from 'writer/console'
|
||||
export {File} from 'writer/file'
|
||||
export {Counter} from 'writer/counter'
|
||||
|
||||
import {Level, type WriterOptions} from 'types'
|
||||
|
||||
export abstract class Writer {
|
||||
protected readonly _options: WriterOptions;
|
||||
protected constructor(options: WriterOptions) {
|
||||
this._options = options
|
||||
}
|
||||
|
||||
public log(level: Level, ...data: any[]) : void {
|
||||
if (level < this.options.minLevel) return
|
||||
this.write(level, ...data)
|
||||
}
|
||||
protected abstract write(level: Level, ...data: any[]) : void;
|
||||
|
||||
public get options() : WriterOptions {
|
||||
return this._options
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user