1.0.30 • Published 5 years ago

js-standard-logger v1.0.30

Weekly downloads
35
License
MIT
Repository
github
Last release
5 years ago

js-standard-logger

A useful standard Logger for JS, based on winston, formatting simple output for log, saving to demanded path. Common output as: [2018-12-11 09:41:19] [info] [sample-label] --- it s info

npm install js-standard-logger

1. Install

$ npm install --save js-standard-logger

2. Usage

const Logger = require('js-standard-logger')
const logger = new Logger('/data/logs/logger-sample/') // simply initialize


logger.exec('it s info') // default for info level output
//[2018-12-11 09:41:19] [info] [sample-label] --- it s info

logger.exec(`${Logger.stringify({it:'can be object',also:['can','be','array']}, true)}`,Logger.WARN()) // choose diff level
//[2019-03-27 14:20:55] [warn] [test] --- {
//  "it": "can be object",
//  "also": [
//    "can",
//     ...
//  ]
//}

logger.exceptionThrows('log and throw err') // do log and throw err, which required try-catch
//[2018-12-11 09:41:19] [error] [sample-label] --- log and throw err

logger.exec('it s error', Logger.ERROR(), new TypeError('get type error')) // log err stack
//[2019-01-29 09:29:43] [error] [test] --- it s error . err stack : TypeError: get type error
//    at Object.<anonymous> (/home/roren/Documents/source/self/logger/test.js:9:45)
//    at Module._compile (internal/modules/cjs/loader.js:686:14)
//    ...

3. Api

Init

Please fill with absolute path.

const Logger = require('js-standard-logger')
const logger = new Logger('/data/logs/log-save-dir/', 'log-name.log','default-label', Logger.INFO()) 
argumentsrequireddescription
log_save_dirtruedir abstract path to save log (auto mkdir if not exists)
log_namefalsedefault to "app.log"
labelfalselabel to differ log, default to log_name
view_levelfalsechoose viewing output level, using Logger fields, default to debug

Function

logger.exec()

Do log on message with different level

argumentsrequireddescription
messagetruemessage to log
levelfalsetag the label according to level field, default to info
errfalseError instance, will logged stack

logger.exceptionThrows()

Do log on message and throw error, which recommended to catch outside

argumentsrequireddescription
messagetruemessage to log
levelfalsetag the label according to level field, default to error

Logger.inspect()

inspect object to view

argumentsrequireddescription
objecttrueinspect object

Logger.stringify()

Transform object to string when logging

argumentsrequireddescription
objecttruestringify object type to do log
prettyfalsepretty json output, default false

Field

Different level fields to choose

FieldsLevelDescription
Logger.DEBUG()debugtags for development debug
Logger.INFO()informationtags for information
Logger.WARN()warningtags for that be bad for some features
Logger.ERROR()errortags for that intercept process or functional crash
1.0.30

5 years ago

1.0.29

5 years ago

1.0.28

5 years ago

1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.23

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago