Skip to content

Commit

Permalink
feat(zhi-lib-log): add a better log system
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed May 5, 2023
1 parent 60e8ba3 commit dfeb043
Show file tree
Hide file tree
Showing 17 changed files with 964 additions and 8 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@
"vite-tsconfig-paths": "^4.0.2",
"vitest": "^0.31.0"
},
"dependencies": {}
"dependencies": {
"kleur": "^4.1.5",
"loglevel": "^1.8.1",
"loglevel-plugin-prefix": "^0.8.4"
}
}
91 changes: 91 additions & 0 deletions packages/zhi-lib-env/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import { describe, expect, it } from "vitest"
import { Env, EnvConstants } from "./index"

describe("zhiEnv", () => {
const NOT_EXIST_KEY = "NOT_EXIST_KEY"

it("test env", () => {
const env = new Env(import.meta.env)
expect(env.getEnv(EnvConstants.NODE_ENV_KEY)).toEqual("test")
})

it("test debug mode", () => {
const env = new Env(import.meta.env)
expect(env.getEnv(EnvConstants.VITE_DEBUG_MODE_KEY)).toEqual("true")
})

it("test getEnv undefined", function () {
const env = new Env(import.meta.env)

const val = env.getEnv(NOT_EXIST_KEY)
console.log("val=>", val)
expect(val).toBeUndefined()
})

it("test getEnv ok", function () {
const env = new Env(import.meta.env)

const val = env.getEnv(EnvConstants.VITE_DEBUG_MODE_KEY)
console.log("val=>", val)
// expect(val).toBeTruthy()
})

it("test getStringEnv", function () {
const env = new Env(import.meta.env)

const val = env.getStringEnv(EnvConstants.VITE_DEBUG_MODE_KEY)
console.log("val=>", val)
expect(typeof val).toBe("string")
})

it("test getBooleanEnv", function () {
const env = new Env(import.meta.env)

const val = env.getBooleanEnv(EnvConstants.VITE_DEBUG_MODE_KEY)
console.log("val=>", val)
expect(typeof val).toBe("boolean")
})

it("test getEnvOrDefault", function () {
const env = new Env(import.meta.env)

const val = env.getEnvOrDefault(NOT_EXIST_KEY, "hello")
console.log("val=>", val)
expect(typeof val).toBe("string")
})

it("test custom env", function () {
const env = new Env({
"mykey-a": "myvalue",
})

const val = env.getEnv("mykey-a")
console.log("val=>", val)
expect(typeof val).toBe("string")
})
})
3 changes: 2 additions & 1 deletion packages/zhi-lib-env/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"target": "esnext",
"module": "esnext",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
Expand Down
81 changes: 81 additions & 0 deletions packages/zhi-lib-log/src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import { describe, it } from "vitest"
import { Env } from "@siyuan-community/zhi-env"
import { LogFactory, LogLevelEnum } from "./index"

describe("zhiLibLog", () => {
it("test default log", function () {
const logger = LogFactory.defaultLogger()
logger.debug("This is debug log")
logger.info("This is info log")
logger.error("This is error log")
})

it("test default log env", function () {
const env = new Env(import.meta.env)
const logger = LogFactory.defaultLogger(env, 4)
logger.debug("This is debug log")
logger.info("This is info log")
logger.error("This is error log")
})

it("test custom sign", function () {
const logger = LogFactory.customSignLogFactory("haha").getLogger()
logger.debug("This is debug log")
logger.info("This is info log")
logger.error("This is error log")
})

it("test default logger", function () {
const logger = LogFactory.customLogFactory().getLogger(undefined, 2)
logger.debug("This is debug log")
logger.info("This is info log")
logger.error("This is error log")
})

it("test custom logger", function () {
const logger = LogFactory.customLogFactory().getLogger("haha")
logger.debug("This is debug log")
logger.info("This is info log")
logger.error("This is error log")
})

it("test custom log level", function () {
const logger = LogFactory.customLogFactory(LogLevelEnum.LOG_LEVEL_TRACE).getLogger("test")
logger.trace("This is trace log")
logger.debug("This is debug log")
logger.info("This is info log")
// logger.error("This is error log")
})

it("test custom level and sign", function () {
const logger = LogFactory.customLogFactory(LogLevelEnum.LOG_LEVEL_DEBUG, "my-log").getLogger("test")
logger.debug("This is debug log")
logger.info("This is info log")
logger.error("This is error log")
})
})
25 changes: 25 additions & 0 deletions packages/zhi-lib-log/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

export * from "./lib/zhi-lib-log"
78 changes: 78 additions & 0 deletions packages/zhi-lib-log/src/lib/crossChalk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

import colors from "ansi-colors"
import kleur from "kleur"
import { BrowserUtil } from "@siyuan-community/zhi-device"

/**
* 跨平台,同时支持Node和浏览器的颜色解决方案
*
* @public
* @author terwer
* @version 1.9.2
* @since 1.9.2
*/
// polyfill due to https://github.com/vitejs/vite/issues/7385
const crossChalk = {
white: (str: string): string => {
return BrowserUtil.isElectron() ? colors.whiteBright(str) : kleur.white(str)
},
gray: (str: string): string => {
return BrowserUtil.isElectron() ? colors.gray(str) : kleur.gray(str)
},
blue: (str: string): string => {
return BrowserUtil.isElectron() ? colors.blue(str) : kleur.blue(str)
},
green: (str: string): string => {
return BrowserUtil.isElectron() ? colors.green(str) : kleur.green(str)
},
yellow: (str: string): string => {
return BrowserUtil.isElectron() ? colors.yellow(str) : kleur.yellow(str)
},
red: (str: string): string => {
return BrowserUtil.isElectron() ? colors.red(str) : kleur.red(str)
},
bgWhite: (str: string): string => {
return BrowserUtil.isElectron() ? colors.bgWhiteBright(str) : kleur.bgWhite(str)
},
bgGrey: (str: string): string => {
return BrowserUtil.isElectron() ? colors.bgCyanBright(str) : kleur.bgCyan(str)
},
bgBlue: (str: string): string => {
return BrowserUtil.isElectron() ? colors.bgBlueBright(str) : kleur.bgBlue(str)
},
bgGreen: (str: string): string => {
return BrowserUtil.isElectron() ? colors.bgGreenBright(str) : kleur.bgGreen(str)
},
bgYellow: (str: string): string => {
return BrowserUtil.isElectron() ? colors.bgYellowBright(str) : kleur.bgYellow(str)
},
bgRed: (str: string): string => {
return BrowserUtil.isElectron() ? colors.bgRedBright(str) : kleur.bgRed(str)
},
}

export default crossChalk
84 changes: 84 additions & 0 deletions packages/zhi-lib-log/src/lib/defaultLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/
import { Logger } from "loglevel"

/**
* 默认日志记录器
*
* @public
* @author terwer
* @since 1.0.7
*/
interface DefaultLogger extends Logger {
/**
* 日志颜色
*/
colors?: string[]

/**
* Output trace message to console.
* This will also include a full stack trace
*
* @param msg - unknown data to log to the console
*/
trace(...msg: unknown[]): void

/**
* Output debug message to console including appropriate icons
*
* @param msg - unknown data to log to the console
*/
debug(...msg: unknown[]): void

/**
* Output debug message to console including appropriate icons
*
* @param msg - unknown data to log to the console
*/
log(...msg: unknown[]): void

/**
* Output info message to console including appropriate icons
*
* @param msg - unknown data to log to the console
*/
info(...msg: unknown[]): void

/**
* Output warn message to console including appropriate icons
*
* @param msg - unknown data to log to the console
*/
warn(...msg: unknown[]): void

/**
* Output error message to console including appropriate icons
*
* @param msg - unknown data to log to the console
*/
error(...msg: unknown[]): void
}

export default DefaultLogger
Loading

0 comments on commit dfeb043

Please sign in to comment.