Skip to content

Commit

Permalink
chore(zhi-log): export crossChalk from zhi-log
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Apr 15, 2023
1 parent b8b2eea commit 3042e2f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 34 deletions.
2 changes: 1 addition & 1 deletion apps/zhi-log/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zhi-log",
"version": "1.9.1",
"version": "1.9.2",
"type": "module",
"description": "a simple logger for Node and Browser",
"main": "./dist/index.js",
Expand Down
2 changes: 2 additions & 0 deletions apps/zhi-log/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import AbstractLogFactory from "./lib/factory/abstractLogFactory"
import CustomLogFactory from "./lib/factory/customLogFactory"
import DefaultLogger from "./lib/defaultLogger"
import EnvHelper from "./lib/envHelper"
import crossChalk from "./lib/crossChalk"

/**
* 日志工具类
Expand Down Expand Up @@ -53,4 +54,5 @@ export default LogFactory

export { LogLevelEnum, AbstractLogFactory, CustomLogFactory }
export { LogConstants, EnvHelper }
export { crossChalk }
export type { DefaultLogger }
77 changes: 77 additions & 0 deletions apps/zhi-log/src/lib/crossChalk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 "zhi-device-detection"

/**
* 跨平台,同时支持Node和浏览器的颜色解决方案
*
* @author terwer
* @version 1.9.2
* @static 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
42 changes: 9 additions & 33 deletions apps/zhi-log/src/lib/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ import callsites, { CallSite } from "callsites"
import EnvHelper from "./envHelper"
import Env from "zhi-env"
import DefaultLogger from "./defaultLogger"
import colors from "ansi-colors"
import kleur from "kleur"
import { BrowserUtil } from "zhi-device-detection"
import crossChalk from "./crossChalk"

/**
* 日志工具类
Expand Down Expand Up @@ -66,36 +64,14 @@ class Logger {
customLevel = customLevel ?? LogLevelEnum.LOG_LEVEL_INFO
loglevel.setLevel(customLevel)

// 颜色
// polyfill due to https://github.com/vitejs/vite/issues/7385
const chalk = {
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)
},
}
const fmtLog = (level: LogLevelEnum, name: string, timestamp: Date, colorFn: any) => {
const strarr = []

// sign
const logSign = sign ?? EnvHelper.getEnvLogger(env) ?? "zhi"
strarr.push(chalk.gray("[") + colorFn(logSign) + chalk.gray("]"))
strarr.push(crossChalk.gray("[") + colorFn(logSign) + crossChalk.gray("]"))
// timestamp
strarr.push(chalk.gray("[") + chalk.gray(timestamp.toString()) + chalk.gray("]"))
strarr.push(crossChalk.gray("[") + crossChalk.gray(timestamp.toString()) + crossChalk.gray("]"))
// level
strarr.push(colorFn(level.toUpperCase().toString()))
// name
Expand All @@ -111,22 +87,22 @@ class Logger {

switch (level) {
case LogLevelEnum.LOG_LEVEL_TRACE:
strarr = fmtLog(level, logName, timestamp, chalk.white)
strarr = fmtLog(level, logName, timestamp, crossChalk.white)
break
case LogLevelEnum.LOG_LEVEL_DEBUG:
strarr = fmtLog(level, logName, timestamp, chalk.gray)
strarr = fmtLog(level, logName, timestamp, crossChalk.gray)
break
case LogLevelEnum.LOG_LEVEL_INFO:
strarr = fmtLog(level, logName, timestamp, chalk.green)
strarr = fmtLog(level, logName, timestamp, crossChalk.green)
break
case LogLevelEnum.LOG_LEVEL_WARN:
strarr = fmtLog(level, logName, timestamp, chalk.yellow)
strarr = fmtLog(level, logName, timestamp, crossChalk.yellow)
break
case LogLevelEnum.LOG_LEVEL_ERROR:
strarr = fmtLog(level, logName, timestamp, chalk.red)
strarr = fmtLog(level, logName, timestamp, crossChalk.red)
break
default:
strarr = fmtLog(LogLevelEnum.LOG_LEVEL_INFO, logName, timestamp, chalk.green)
strarr = fmtLog(LogLevelEnum.LOG_LEVEL_INFO, logName, timestamp, crossChalk.green)
break
}

Expand Down

0 comments on commit 3042e2f

Please sign in to comment.