Created
February 12, 2020 12:18
-
-
Save alanwei43/5424b5bfd7bcd519afbcbda5a3ca30f5 to your computer and use it in GitHub Desktop.
logger module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { appendFile, existsSync, mkdirSync } from "fs"; | |
import { join } from "path"; | |
let GLOBAL_COUNT = 0; | |
export class Log { | |
private dir: string | |
constructor(private name: string) { | |
this.dir = join(process.cwd(), "logs"); | |
this.checkDir(); | |
} | |
checkDir() { | |
if (!existsSync(this.dir)) { | |
mkdirSync(this.dir, { recursive: true }); | |
} | |
} | |
private write(level: string, ...args: any[]): void { | |
const filePath = join(this.dir, `${this.name}.${level}.log`); | |
args.unshift(`[${GLOBAL_COUNT++} ${new Date().toLocaleString()}]`); | |
args.push("\n"); | |
appendFile(filePath, args.join(" "), { | |
encoding: "utf8" | |
}, (err) => { | |
if (err) { | |
console.warn(`write file(${filePath}) failed: `, err); | |
this.checkDir(); | |
} | |
}); | |
} | |
debug(...args: any[]): Log { | |
this.write("debug", Array.from(arguments)); | |
return this; | |
} | |
warn(...args: any[]): Log { | |
this.write("warn", Array.from(arguments)); | |
return this; | |
} | |
error(...args: any[]): Log { | |
this.write("error", args); | |
return this; | |
} | |
} | |
export const Logger: Log = new Log("default"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment