npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chinese-room

v0.0.6

Published

Logger with color output to the console, timestamp support and the ability to write to a file.

Downloads

2

Readme

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

| 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.