0.2.1 • Published 4 years ago

@barusu/chalk-logger v0.2.1

Weekly downloads
167
License
MIT
Repository
github
Last release
4 years ago

chalk-logger is a colorful logger tool based on chalk(so you can use a lot of colors) and commander(so you can use command line parameters to customized the logger's behavior).

Install

  • npm

    npm install --save @barusu/chalk-logger
  • yarn

    yarn add @barusu/chalk-logger

Usage

Options

NameTypeRequiredDefaultDesc
basenamestring | nullfalsenullsee below
mode'normal' | 'loose'falsenormalsee below
placeholderRegexRegExpfalse/(?<!\\)\{\}/gstring formatter placeholder regex
namestringfalse-name of logger
levelLevelfalseLevel.INFOverbosity level of the logging output
datebooleanfalsefalsewhether to print the date
titlebooleanfalsetruewhether to print the title
inlinebooleanfalsefalsewhether to print each log on one line
colorfulbooleanfalsetruewhether to print log surrounded with color
encodingstringfalseutf-8see below
filepathstringfalse-see below
write(text: string) => voidfalseprocess.stdoutsee below
dateChalkChalk | Colorfalsechalk.graycolor of date string
nameChalkChalk | Colorfalsechalk.graycolor of logger name string

Option Details

  • basename: Base of the logger name, when you change logger name according setName function, the basename will be prefixed of the logger name

  • mode

    • normal: Print log only
    • loose: Print a newline before and after the log
  • encoding: Specifying the file encoding of filepath, only works if filepath is specified

  • filepath: Log output file

  • write: If filepath is specified, the log is output to filepath by default, otherwise to the process.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 prefix no- represent negation.
    • date: whether to print date. default value is false
    • title: whether to print title. default value is true
    • inline: 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 and inline = true if you want to output logs into a file.
  • --log-encoding <encoding>: specify the log file's encoding.

Examples

  • Basic:

    // demo/demo1.ts
    import { ChalkLogger, ERROR } from 'chalk-logger'
    
    const logger = new ChalkLogger({
      name: 'demo',
      level: ERROR,   // the default value is INFO
      date: false,    // the default value is false.
      colorful: true, // the default value is true.
    }, process.argv)

logger.debug('A', 'B', 'C') logger.verbose('A', 'B', 'C') logger.info('a', 'b', 'c') logger.warn('X', 'Y', 'Z', { a: 1, b: 2}) logger.error('x', 'y', 'z', { c: { a: 'hello' }, b: { d: 'world' } }) logger.fatal('1', '2', '3')

![demo1.1.png](https://raw.githubusercontent.com/guanghechen/barusu/main/packages/chalk-logger/screenshots/demo1.1.png)

* Custom output format:

```typescript
// demo/demo2.ts
import chalk from 'chalk'
import { ChalkLogger, ERROR, Level } from 'chalk-logger'

let logger = new ChalkLogger({
  name: 'demo',
  level: ERROR,   // the default value is INFO
  date: false,    // the default value is false.
  colorful: true, // the default value is true.
}, process.argv)

logger.formatHeader = function (level: Level, date: Date): string {
  let { desc } = level
  let { name } = this
  if( this.flags.colorful ) {
    desc = level.headerChalk.fg(desc)
    if (level.headerChalk.bg != null) desc = level.headerChalk.bg(desc)
    name = chalk.gray(name)
  }
  let header = `${desc} ${name}`
  if( !this.flags.date) return `[${header}]`

  let dateString = date.toLocaleTimeString()
  if( this.flags.colorful ) dateString = chalk.gray(dateString)
  return `<${dateString} ${header}>`
}

logger.debug('A', 'B', 'C')
logger.verbose('A', 'B', 'C')
logger.info('a', 'b', 'c')
logger.warn('X', 'Y', 'Z', { a: 1, b: 2})
logger.error('x', 'y', 'z', { c: { a: 'hello' }, b: { d: 'world' } })
logger.fatal('1', '2', '3')

demo2.1.png

  • Custom output format (2):

    // demo/demo3.ts
    import chalk from 'chalk'
    import { ChalkLogger, ERROR } from 'chalk-logger'
    
    let logger = new ChalkLogger({
      name: 'demo',
      level: ERROR,     // the default value is INFO
      date: false,      // the default value is false.
      colorful: true,   // the default value is true.
      dateChalk: 'green',
      nameChalk: chalk.cyan.bind(chalk),
    }, process.argv)
    
    logger.debug('A', 'B', 'C')
    logger.verbose('A', 'B', 'C')
    logger.info('a', 'b', 'c')
    logger.warn('X', 'Y', 'Z', { a: 1, b: 2})
    logger.error('x', 'y', 'z', { c: { a: 'hello' }, b: { d: 'world' } })
    logger.fatal('1', '2', '3')

    demo3.1.png

  • Output to file

    // demo/demo4.ts
    import path from 'path'
    import chalk from 'chalk'
    import { ChalkLogger, DEBUG } from 'chalk-logger'
    
    let logger = new ChalkLogger({
      name: 'demo',
      level: DEBUG,       // the default value is DEBUG
      date: true,         // the default value is false.
      inline: true,
      colorful: false,    // the default value is true.
      dateChalk: 'green',
      nameChalk: chalk.cyan.bind(chalk),
      filepath: path.resolve(__dirname, 'orz.log'),
      encoding: 'utf-8',
    }, process.argv)
    
    logger.debug('A', 'B', 'C')
    logger.verbose('A', 'B', 'C')
    logger.info('a', 'b', 'c')
    logger.warn('X', 'Y', 'Z', { a: 1, b: 2})
    logger.error('x', 'y', 'z', { c: { a: 'hello' }, b: { d: 'world' } })
    logger.fatal('1', '2', '3')

    demo4.1.png

  • Register to commander:

    // demo/demo5.ts
    import chalk from 'chalk'
    import commander from 'Commander'
    import { ChalkLogger, ERROR } from '../src'
    
    let logger = new ChalkLogger({
      name: 'demo',
      level: ERROR,     // the default value is INFO
      date: false,      // the default value is false.
      colorful: true,   // the default value is true.
      dateChalk: 'green',
      nameChalk: chalk.cyan.bind(chalk),
    }, process.argv)
    
    commander
      .version('v1.0.0')
      .arguments('[orz]')
    
    // register logger option to commander
    logger.registerToCommander(commander)
    // or ChalkLogger.registerToCommander(commander)
    
    commander
      .option('-e, --encoding <encoding>', 'specified <filepath>\'s encoding')
      .parse(process.argv)
    
    logger.debug('A', 'B', 'C')
    logger.verbose('A', 'B', 'C')
    logger.info('a', 'b', 'c')
    logger.warn('X', 'Y', 'Z', { a: 1, b: 2})
    logger.error('x', 'y', 'z', { c: { a: 'hello' }, b: { d: 'world' } })
    logger.fatal('1', '2', '3')

    demo5.1.png

  • String format:

    // demo/demo6.ts
    import { ChalkLogger, DEBUG } from '../src'
    
    let logger = new ChalkLogger({
      name: 'demo',
      level: DEBUG,
      date: true,
      colorful: true,
      inline: true,
    }, process.argv)
    
    logger.verbose('user({})', { username: 'guanghechen', avatar: 'https://avatars0.githubusercontent.com/u/42513619?s=400&u=d878f4532bb5749979e18f3696b8985b90e9f78b&v=4' })
    logger.error('bad argument ({}). error({})', { username: 123 }, new Error('username is invalid'))
    
    let logger2 = new ChalkLogger({
      name: 'demo',
      level: DEBUG,
      date: true,
      colorful: true,
      inline: true,
      placeholderRegex: /(?<!\\)\<\>/g  // change placeholder of string format
    }, process.argv)
    
    logger2.verbose('user(<>)', { username: 'guanghechen', avatar: 'https://avatars0.githubusercontent.com/u/42513619?s=400&u=d878f4532bb5749979e18f3696b8985b90e9f78b&v=4' })
    logger2.error('bad argument (<>). error({})', { username: 123 }, new Error('username is invalid'))

    demo6.1.png

0.2.1

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.48

4 years ago

0.0.47

4 years ago

0.0.46

4 years ago

0.0.45

4 years ago

0.0.44

5 years ago

0.0.43

5 years ago

0.0.42

5 years ago

0.0.41

5 years ago

0.0.40

5 years ago

0.0.39

5 years ago

0.0.38

5 years ago

0.0.37

5 years ago

0.0.36

5 years ago

0.0.35

5 years ago

0.0.34

5 years ago

0.0.33

5 years ago

0.0.32

5 years ago

0.0.31

5 years ago

0.0.30

5 years ago

0.0.29

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.19

5 years ago

0.0.18

5 years ago

0.0.16

5 years ago

0.0.17

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.10

5 years ago

0.0.11

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago