-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zhi-lib-log): add a better log system
- Loading branch information
Showing
17 changed files
with
964 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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") | ||
}) | ||
}) |
This file contains 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
This file contains 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
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") | ||
}) | ||
}) |
This file contains 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
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" |
This file contains 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
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 |
This file contains 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
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 |
Oops, something went wrong.