feat: add logger package

This commit is contained in:
Skillz4Killz
2022-08-26 17:37:32 +00:00
committed by GitHub
parent 9183ade29d
commit 23dfe04dfa
5 changed files with 119 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ on:
- "handlers/**"
- "helpers/**"
- "plugins/**"
- "packages/**"
- "rest/**"
- "tests/**"
- "transformers/**"

View File

@@ -8,6 +8,7 @@
"./handlers",
"./helpers",
"./plugins",
"./packages",
"./rest",
"./template/beginner",
"./template/minimal",

14
dnt.ts
View File

@@ -41,6 +41,14 @@ await build({
name: "./transformers",
path: "transformers/mod.ts",
},
{
name: "./packages",
path: "packages/mod.ts",
},
{
name: "./logger",
path: "packages/logger/mod.ts",
},
{
name: "./plugins",
path: "plugins/mod.ts",
@@ -108,6 +116,12 @@ await build({
"transformers": [
"./types/transformers/mod.d.ts",
],
"packages": [
"./types/packages/mod.d.ts",
],
"logger": [
"./types/packages/logger/mod.d.ts",
],
"plugins": [
"./types/plugins/mod.d.ts",
],

1
packages/logger/deps.ts Normal file
View File

@@ -0,0 +1 @@
export * from "https://deno.land/std@0.153.0/fmt/colors.ts";

102
packages/logger/mod.ts Normal file
View File

@@ -0,0 +1,102 @@
import { bold, cyan, gray, italic, red, yellow } from "./deps.ts";
export enum LogLevels {
Debug,
Info,
Warn,
Error,
Fatal,
}
const prefixes = new Map<LogLevels, string>([
[LogLevels.Debug, "DEBUG"],
[LogLevels.Info, "INFO"],
[LogLevels.Warn, "WARN"],
[LogLevels.Error, "ERROR"],
[LogLevels.Fatal, "FATAL"],
]);
const noColor: (str: string) => string = (msg) => msg;
const colorFunctions = new Map<LogLevels, (str: string) => string>([
[LogLevels.Debug, gray],
[LogLevels.Info, cyan],
[LogLevels.Warn, yellow],
[LogLevels.Error, (str: string) => red(str)],
[LogLevels.Fatal, (str: string) => red(bold(italic(str)))],
]);
export function logger({
logLevel = LogLevels.Info,
name,
}: {
logLevel?: LogLevels;
name?: string;
} = {}) {
function log(level: LogLevels, ...args: any[]) {
if (level < logLevel) return;
let color = colorFunctions.get(level);
if (!color) color = noColor;
const date = new Date();
const log = [
`[${date.toLocaleDateString()} ${date.toLocaleTimeString()}]`,
color(prefixes.get(level) || "DEBUG"),
name ? `${name} >` : ">",
...args,
];
switch (level) {
case LogLevels.Debug:
return console.debug(...log);
case LogLevels.Info:
return console.info(...log);
case LogLevels.Warn:
return console.warn(...log);
case LogLevels.Error:
return console.error(...log);
case LogLevels.Fatal:
return console.error(...log);
default:
return console.log(...log);
}
}
function setLevel(level: LogLevels) {
logLevel = level;
}
function debug(...args: any[]) {
log(LogLevels.Debug, ...args);
}
function info(...args: any[]) {
log(LogLevels.Info, ...args);
}
function warn(...args: any[]) {
log(LogLevels.Warn, ...args);
}
function error(...args: any[]) {
log(LogLevels.Error, ...args);
}
function fatal(...args: any[]) {
log(LogLevels.Fatal, ...args);
}
return {
log,
setLevel,
debug,
info,
warn,
error,
fatal,
};
}
export const log = logger({ name: "Main" });
export const createLogger = logger;
export default log;