0.0.6 • Published 3 years ago

chinese-room v0.0.6

Weekly downloads
6
License
MIT
Repository
github
Last release
3 years ago

Table of Contents

  1. Install
  2. Usage
  3. Options
  4. Examples of using
  5. API
  6. Community

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. If true, the directory or file will be recreated and logs will be written there.
    • Type: Boolean
    • Default: false
    • ====
  • writeToFile - If true, logs will be written to a file.
    • ====
    • Type: Boolean
    • Default: false
    • ====
  • writeToConsole - If true, logs will be written to console.
    • ====
    • Type: Boolean
    • Default: true
    • ====
  • useColoredOutput - If true, the console output will be colored. Errors in red, logs in blue, timestamp in yellow.
    • ====
    • Type: Boolean
    • Default: true
    • ====
  • useTimestamp - If true, 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

MethodParamReturnDescription
logger.logStringPromise - if writeToFile = true, otherwise - undefined.Outputs to the console and/or writes to a file the given string.
logger.errorStringPromise - if writeToFile = true, otherwise - undefined.Outputs to the console and/or writes to a file the given string.
logger.getOptionsObjectReturns an object with logger options.
logger.setDefaultOptionsundefinedSets all logger options to default.
logger.clearAllLogsPromiseClears all log files.
logger.clearErrorFilePromiseCleans up the file with errors.
logger.clearLogFilePromiseClean up the file with logs.

Community

Criticism, corrections, improvements, suggestions are welcome.