0.0.6 • Published 3 years ago
chinese-room v0.0.6
Table of Contents
Install
Install with npm:
npm i chinese-room
Usage
const ChineseRoom = require('chinese-room')
const logger = new ChineseRoom({
// Options
})
logger.log('Hello world.') // > [2021-01-10T15:12:08.464Z] [LOG] Hello world.
logger.error('Hello world.') // > [2021-01-10T15:12:08.464Z] [ERROR] Hello world.
To view the available options, see Options.
Options
logsOutputDirectory
- the path to the directory where the log files will be stored.- ====
- Type:
String
- Default:
path.resolve() + '/logs/'
- ====
watchLogsDirectory
- whether to check of the directory or file exists before writing logs to a file.- ====
- Note: If during execution a directory or file with logs is accidentally or intentionally deleted, then if
false
, an error will be throw during the writing of logs to the file. Iftrue
, the directory or file will be recreated and logs will be written there. - Type:
Boolean
- Default:
false
- ====
writeToFile
- Iftrue
, logs will be written to a file.- ====
- Type:
Boolean
- Default:
false
- ====
writeToConsole
- Iftrue
, logs will be written to console.- ====
- Type:
Boolean
- Default:
true
- ====
useColoredOutput
- Iftrue
, the console output will be colored. Errors in red, logs in blue, timestamp in yellow.- ====
- Type:
Boolean
- Default:
true
- ====
useTimestamp
- Iftrue
, a timestamp will be inserted before of logs and errors.- ====
- Type:
Boolean
- Default:
true
- ====
errorFileName
- the name of the file to which errors will be written.- ====
- Type:
String
- Default:
error.txt
- ====
logFileName
- the name of the file to which logs will be written.- ====
- Type:
String
- Default:
log.txt
- ====
Examples of using
Basic example with express
:
const express = require('express')
const ChineseRoom = require('chinese-room')
const server = express()
// Initialize logger
const logger = new ChineseRoom()
server.get('*', (req, res) => {
// Write log
logger.log(req.url)
res.json({ ok: true })
})
server.listen(3000)
The console output will be as follow: [2021-01-10T15:12:08.464Z] [LOG] /home
.
To write the log to a file, use the writeToFile
option:
const express = require('express')
const ChineseRoom = require('chinese-room')
const server = express()
// Initialize logger
const logger = new ChineseRoom({
writeToFile: true, // * Just add this option
})
server.get('*', (req, res) => {
// Write log
// * This log will be written to file.
logger.log(req.url)
res.json({ ok: true })
})
server.listen(3000)
After that, a directory with the log.txt
file will be created in the project root, if it doesn't exist.
To write to a file or print an error to the console, use the error
method:
server.use((serverError, req, res, next) => {
// Write error
logger.error(serverError)
res.status(500).end('Server error. 500.')
})
To clear all log files use:
logger.clearAllLogs()
See more in Methods.
API
Methods
Method | Param | Return | Description |
---|---|---|---|
logger.log | String | Promise - if writeToFile = true , otherwise - undefined . | Outputs to the console and/or writes to a file the given string. |
logger.error | String | Promise - if writeToFile = true , otherwise - undefined . | Outputs to the console and/or writes to a file the given string. |
logger.getOptions | Object | Returns an object with logger options. | |
logger.setDefaultOptions | undefined | Sets all logger options to default. | |
logger.clearAllLogs | Promise | Clears all log files. | |
logger.clearErrorFile | Promise | Cleans up the file with errors. | |
logger.clearLogFile | Promise | Clean up the file with logs. |
Community
Criticism, corrections, improvements, suggestions are welcome.