README
@guanghechen/chalk-logger
chalk-logger
is a colorful logger tool based on chalk (so you can use a
lot of colors), and can be easily integrated into commander.js (so you can
use command line parameters to customized the logger's behavior).
Install
npm
npm install --save @guanghechen/chalk-logger
yarn
yarn add @guanghechen/chalk-logger
Usage
Options
Name | Type | Required | Default | Desc |
---|---|---|---|---|
basename |
string | null |
false |
null |
see below |
mode |
'normal' | 'loose' |
false |
normal |
see below |
placeholderRegex |
RegExp | false |
/(?<!\\)\{\}/g |
string formatter placeholder regex |
name |
string | false |
- | name of logger |
level |
Level | false |
Level.INFO |
verbosity level of the logging output |
date |
boolean | false |
false |
whether to print the date |
title |
boolean | false |
true |
whether to print the title |
inline |
boolean | false |
false |
whether to print each log on one line |
colorful |
boolean | false |
true |
whether to print log surrounded with color |
encoding |
string | false |
utf-8 |
see below |
filepath |
string | false |
- | see below |
write |
(text: string) => void | false |
process.stdout |
see below |
dateChalk |
Chalk | Color | false |
chalk.gray |
color of date string |
nameChalk |
Chalk | Color | false |
chalk.gray |
color of logger name string |
Option Details
basename
: Base of the logger name, when you change logger name accordingsetName
function, the basename will be prefixed of the logger name.mode
normal
: Print log onlyloose
: Print a newline before and after the log
encoding
: Specifying the file encoding offilepath
, only works iffilepath
is specifiedfilepath
: Log output filewrite
: Iffilepath
is specified, the log is output tofilepath
by default, otherwise to theprocess.stdout
. You can specify your own write function to customize the output behavior of the log.
Cli Options
--log-level <debug|verbose|info|warn|error|fatal>
: specify global logger level.--log-name <new logger name>
: specify global logger name.--log-mode <'normal' | 'loose'>
: specify global logger mode.--log-flag <[no-](date|title|inline|colorful)>
: the prefixno-
represent negation.date
: whether to print date. default value is falsetitle
: whether to print title. default value is trueinline
: each log record output in one line. default value is false.colorful
: whether to print with colors. default value is true.
--log-output <filepath>
: specify the output path (default behavior is output directory to stdout).- suggest: set
colorful = false
andinline = true
if you want to output logs into a file.
- suggest: set
--log-encoding <encoding>
: specify the log file's encoding.
Test
Use [@guanghechen/jest-helper][] to spy logger.
import {
composeStringDesensitizers,
createFilepathDesensitizer,
createJsonDesensitizer,
createLoggerMock,
} from '@guanghechen/jest-helper'
const workspaceRootDir = path.resolve(__dirname, '..')
const desensitize: Desensitizer<unknown[]> = createJsonDesensitizer({
string: composeStringDesensitizers(
createFilepathDesensitizer(workspaceRootDir, '<$WORKSPACE