包详细信息

caterpillar

bevry9.4kArtistic-2.08.2.0

Caterpillar is the ultimate logging system for Deno, Node.js, and Web Browsers. Log levels are implemented to the RFC standard. Log entries can be filtered and piped to various streams, including coloured output to the terminal, the browser's console, and

browser, caterpillar, console, debug

自述文件

Caterpillar

Status of the GitHub Workflow: bevry NPM version NPM downloads
GitHub Sponsors donate button ThanksDev donate button Patreon donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button
Discord server badge Twitch community badge

Caterpillar is the ultimate logging system for Deno, Node.js, and Web Browsers. Log levels are implemented to the RFC standard. Log entries can be filtered and piped to various streams, including coloured output to the terminal, the browser's console, and debug files. You can even write your own transforms.

Usage

Complete API Documentation.

Examples

Overview

The RFC Log Levels are provided by the rfc-log-levels package which follows RFC 3164 - The BSD Syslog Protocol.

Log Entries that are within the lineLevel range, will have their line information fetched using the get-current-line package.

The Logger is what you write your log messages to, which you then pipe to destinations and transforms.

The Filter transport is used to filter out log levels that we do not want to pass onto the next destination.

The Human transport is used to convert the Log Entries into a human readable and colourful output.

The Browser transport is used to send the human output, including colours, to the Web Browser console.

The Transform is used to write your own transforms, and is what all the others are based from.

Node.js Guide

To get started for Node.js, setup a new Node.js project for this guide and install Caterpillar.

mkdir caterpillar-guide
cd caterpillar-guide
npm init
npm install --save caterpillar
touch index.js

Then edit our index.js file with the following, that will output all the log messages in JSON format to stdout, and can be run via node index.js:

const { Logger } = require('caterpillar')
const logger = new Logger()

logger.pipe(process.stdout)

logger.log('warn', 'this is a warning, which is level', 4)
logger.warn('this is a warning, which is level', 4)
logger.log('debug', 'this is a debug message, which is level', 7)
logger.warn('this is a debug message, which is level', 7)

Outputting in JSON format is not a nice experience, instead we can do better by using the Human transport such that it is human readable.

const { Logger, Human } = require('caterpillar')
const logger = new Logger()

logger.pipe(new Human()).pipe(process.stdout)

logger.log('warn', 'this is a warning, which is level', 4)
logger.warn('this is a warning, which is level', 4)
logger.log('debug', 'this is a debug message, which is level', 7)
logger.warn('this is a debug message, which is level', 7)

However, perhaps we want to still store the JSON format for querying later. We can pipe the human format to stdout as before, but we can pipe the raw output to a debug file.

const { Logger, Human } = require('caterpillar')
const logger = new Logger()

const { createWriteStream } = require('fs')
logger.pipe(createWriteStream('./debug.log'))

logger.pipe(new Human()).pipe(process.stdout)

logger.log('warn', 'this is a warning, which is level', 4)
logger.warn('this is a warning, which is level', 4)
logger.log('debug', 'this is a debug message, which is level', 7)
logger.warn('this is a debug message, which is level', 7)

Now let's stay for some reason, we want to capitalise all the log messages that are warning levels and higher, we can do this by making our own transport by extending the Transform.

const { Logger, Transform, Human } = require('caterpillar')
const logger = new Logger()

const { createWriteStream } = require('fs')
logger.pipe(createWriteStream('./debug.log'))

class Uppercase extends Transform {
    format(entry) {
        if (entry.levelNumber <= 4) {
            entry.args.forEach(function (value, index) {
                if (typeof value === 'string') {
                    entry.args[index] = value.toUpperCase()
                }
            })
        }
        return entry
    }
}

logger.pipe(new Uppercase()).pipe(new Human()).pipe(process.stdout)

logger.log('warn', 'this is a warning, which is level', 4)
logger.warn('this is a warning, which is level', 4)
logger.log('debug', 'this is a debug message, which is level', 7)
logger.warn('this is a debug message, which is level', 7)

Futhermore, the user probably doesn't need to see debug messages, even though they are useful for debugging. We can filter out the debug messages for the user, but maintain them for the debug.log file by applying the Filter transport to the pipe that goes to stdout.

const { Logger, Transform, Filter, Human } = require('caterpillar')
const logger = new Logger()

const { createWriteStream } = require('fs')
logger.pipe(createWriteStream('./debug.log'))

class Uppercase extends Transform {
    format(entry) {
        if (entry.levelNumber <= 4) {
            entry.args.forEach(function (value, index) {
                if (typeof value === 'string') {
                    entry.args[index] = value.toUpperCase()
                }
            })
        }
        return entry
    }
}

logger
    .pipe(new Filter({ filterLevel: 5 }))
    .pipe(new Uppercase())
    .pipe(new Human())
    .pipe(process.stdout)

logger.log('warn', 'this is a warning, which is level', 4)
logger.warn('this is a warning, which is level', 4)
logger.log('debug', 'this is a debug message, which is level', 7)
logger.warn('this is a debug message, which is level', 7)

As fetching line information is computationally expensive process, for large applications for performance we probably only want to fetch the line information for messages that we actually show to the user. As such, we should make the filterLevel and the lineLevel the same.

const { Logger, Transform, Filter, Human } = require('caterpillar')
const level = 5
const logger = new Logger({ lineLevel: level })

const { createWriteStream } = require('fs')
logger.pipe(createWriteStream('./debug.log'))

class Uppercase extends Transform {
    format(entry) {
        if (entry.levelNumber <= 4) {
            entry.args.forEach(function (value, index) {
                if (typeof value === 'string') {
                    entry.args[index] = value.toUpperCase()
                }
            })
        }
        return entry
    }
}

logger
    .pipe(new Filter({ filterLevel: 5 }))
    .pipe(new Uppercase())
    .pipe(new Human())
    .pipe(process.stdout)

logger.log('warn', 'this is a warning, which is level', 4)
logger.warn('this is a warning, which is level', 4)
logger.log('debug', 'this is a debug message, which is level', 7)
logger.warn('this is a debug message, which is level', 7)

Finally, if we are using Caterpillar in web browser environments, instead of Node.js, instead of doing:

const { Logger, Transform, Filter, Human } = require('caterpillar')
// ...
logger.pipe(new Human()).pipe(process.stdout)
// ...

We would pipe to the Browser transform instead of to stdout.

const { Logger, Transform, Filter, Human, Browser } = require('caterpillar')
// ...
logger.pipe(new Human()).pipe(new Browser())
// ...

With this, you now have enough information to leverage the cross-platform power of Caterpillar for most purposes, and the power to write your own custom transforms which can be published as their own packages and shared.

Install

npm

  • Install: npm install --save caterpillar
  • Import: import * as pkg from ('caterpillar')
  • Require: const pkg = require('caterpillar')

Deno

import * as pkg from 'https://unpkg.com/caterpillar@^8.2.0/edition-deno/index.ts'

Skypack

<script type="module">
    import * as pkg from '//cdn.skypack.dev/caterpillar@^8.2.0'
</script>

unpkg

<script type="module">
    import * as pkg from '//unpkg.com/caterpillar@^8.2.0'
</script>

jspm

<script type="module">
    import * as pkg from '//dev.jspm.io/caterpillar@8.2.0'
</script>

Editions

This package is published with the following editions:

  • caterpillar aliases caterpillar/index.cjs which uses the Editions Autoloader to automatically select the correct edition for the consumer's environment
  • caterpillar/source/index.ts is TypeScript source code with Import for modules
  • caterpillar/edition-browsers/index.js is TypeScript compiled against ES2022 for web browsers with Import for modules
  • caterpillar/edition-es2022/index.js is TypeScript compiled against ES2022 for Node.js 14 || 16 || 18 || 20 || 21 with Require for modules
  • caterpillar/edition-es2017/index.js is TypeScript compiled against ES2017 for Node.js 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with Require for modules
  • caterpillar/edition-es2015/index.js is TypeScript compiled against ES2015 for Node.js 6 || 8 || 10 || 12 || 14 || 16 || 18 || 20 || 21 with Require for modules
  • caterpillar/edition-es5/index.js is TypeScript compiled against ES5 for Node.js 4 || 6 || 8 || 10 || 12 || 14 || 16 with Require for modules
  • caterpillar/edition-es2017-esm/index.js is TypeScript compiled against ES2017 for Node.js 12 || 14 || 16 || 18 || 20 || 21 with Import for modules
  • caterpillar/edition-types/index.d.ts is TypeScript compiled Types with Import for modules
  • caterpillar/edition-deno/index.ts is TypeScript source code made to be compatible with Deno

History

Discover the release history by heading on over to the HISTORY.md file.

Backers

Code

Discover how to contribute via the CONTRIBUTING.md file.

Authors

Maintainers

Contributors

Finances

GitHub Sponsors donate button ThanksDev donate button Patreon donate button Liberapay donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button

Sponsors

  • Andrew Nesbitt — Software engineer and researcher
  • Balsa — We're Balsa, and we're building tools for builders.
  • Codecov — Empower developers with tools to improve code quality and testing.
  • Poonacha Medappa
  • Rob Morris
  • Sentry — Real-time crash reporting for your web apps, mobile apps, and games.
  • Syntax — Syntax Podcast

Donors

License

Unless stated otherwise all works are:

and licensed under:

更新日志

History

v8.2.0 2023 December 29

v8.1.0 2023 December 27

v8.0.0 2023 December 6

  • Updated dependencies, base files, and editions using boundation
  • Minimum required Node.js version changed from node: >=6 to node: >=4 adapting to ecosystem changes

v7.0.0 2023 November 24

  • Updated dependencies, base files, and editions using boundation
  • Minimum required Node.js version changed from node: >=10 to node: >=6 adapting to ecosystem changes

v6.12.0 2023 November 21

v6.11.0 2023 November 15

v6.10.0 2023 November 14

v6.9.0 2023 November 14

v6.8.0 2021 July 30

v6.7.0 2021 July 29

v6.6.0 2020 October 29

v6.5.0 2020 September 4

v6.4.0 2020 August 18

v6.3.0 2020 August 18

v6.2.0 2020 August 18

v6.1.0 2020 August 4

v6.0.2 2020 July 22

  • Updated Transform documentation and renamed Writeable internal type to Pipeable to reflect its purpose better

v6.0.1 2020 July 22

  • Updated dependencies, fixes Node.js due to missing semver dependency under editions

v6.0.0 2020 July 24

  • Breaking Changes: Caterpillar has been rewritten for performance, ease of use, and deno compatibility (now it is compatible with Node.js, Deno, and Web Browsers)
  • The Caterpillar Transforms by Bevry, are now embedded into the caterpillar package:
    • caterpillar-filter is now import { Filter } from 'caterpillar'
    • caterpillar-human is now import { Human } from 'caterpillar'
    • caterpillar-browser is now import { Browser } from 'caterpillar'
  • *.create() aliases for new *() are now removed, please just use new *(config)
  • Each Caterpillar Transform now maintains its own configuration, which is specified via their constructor
    • As such, instead of doing new *().setConfig(opts) now jsut do new *(opts)
  • the default log level is now a configuration option, rather than an entry in the levels map
  • level configuration option has been renamed to filterLevel, which must now be specified directly on the filter transform
  • the logger transform now accepts a new lineLevel configuration option, which will limit fetching line info for only log levels equal to or below that the lineLevel value, by default it is -1 which disables fetching line info
    • This is a dramatic performance improvement for large applications, as fetching line levels for every log entry, even ones filtered out, was not performant
    • Closes issue #16
  • Caterpillar Transforms are no longer Node.js Streams, as using them was a major performance overhead
    • We can still pipe to Caterpillar Transforms as well as to Node.js streams and WHATWG/Deno streams
    • Closes issue #17
  • Add error, warn, info, debug aliases to the logger
  • Updated dependencies, base files, and editions using boundation

v5.15.0 2020 July 22

v5.14.0 2020 July 22

v5.13.0 2020 July 3

v5.12.0 2020 July 3

v5.11.0 2020 June 25

v5.10.0 2020 June 21

v5.9.0 2020 June 21

v5.8.0 2020 June 20

v5.7.0 2020 June 20

v5.6.0 2020 June 10

v5.5.0 2020 June 10

v5.4.0 2020 May 22

v5.3.0 2020 May 21

v5.2.0 2020 May 11

v5.1.2 2020 May 8

  • Added Logger as a default export, for better compat with the filters

v5.1.1 2020 May 8

  • Fix some types on the Transform class

v5.1.0 2020 May 8

  • Merge source code into a single file, and export the various types

v5.0.0 2020 May 8

  • Converted from JavaScript to TypeScript, no functionality changes here
  • Extracted the current line functionality into get-current-line, which uses a different means of calculating the offset which you should refer to if you ever used custom offsets
  • Extracted the log level functionality into rfc-log-levels, existing functionality is retained
  • Updated dependencies, base files, and editions using boundation
  • Minimum required node version changed from node: >=8 to node: >=10 to keep up with mandatory ecosystem changes

v4.0.0 2019 December 1

  • Updated dependencies, base files, and editions using boundation
  • Minimum required node version changed from node: >=0.10 to node: >=8 to keep up with mandatory ecosystem changes

v3.3.0 2019 November 13

v3.2.0 2019 January 1

v3.1.2 2018 September 3

v3.1.1 2018 August 19

  • Readded support for node 0.10

v3.1.0 2018 August 17

  • Now uses rfc-log-levels for the initial log levels
  • Moved type linting from flow to jsdoc & typescript, which also results in better documentation for you, and visual studio code intellisense`
  • Updated base files and editions using boundation

v3.0.1 2016 October 20

  • Fixed flow type errors with newer flow versions

v3.0.0 2016 May 4

  • Converted from CoffeeScript to JavaScript
  • .createLogger() and .createTransform() now removed in favour of Logger.create() and Transform.create()
  • require('caterpillar').create() alias added
  • Logger no longer inherits from Transform

v2.0.9 2015 February 18

  • Fixed an issue when fetching (new Error()).stack would fail
  • More robust stack parsing

v2.0.8 2015 February 7

  • Updated dependencies

v2.0.7 2013 December 12

  • Use native streams if available, otherwise fallback to readable-stream
  • Repackaged

v2.0.6 2013 October 23

  • Logger:log is now permantely bound to the logger instance, for easy passing around

v2.0.5 2013 October 23

  • Added create API to make life easier when doing one liners
  • Project meta data files are now maintained by Projectz
  • Updated dependencies

v2.0.4 2013 July 23

  • Added lineOffset configuration offset to allow you to detect the correct line of the reporting when using wrappers
  • Updated dependencies

v2.0.3 2013 May 19

  • iOS support (iOS devices do not have new Error().stack)

v2.0.2 2013 May 7

v2.0.1 2013 April 25

  • Node 0.8 support

v2.0.0 2013 April 25

  • Rewrote using streams

v1.1.4 2013 March 25

  • Repackaged

v1.1.3 2012 October 18

  • Updated cli-color from 0.1 to 0.2
  • Make cli-color an optional dependency

v1.1.2 2012 August 10

  • Rejigged directory structure
  • Re-added markdown files to npm distribution as they are required for the npm website

v1.1.1 2012 May 4

  • Fixed dependency overwrite

v1.1.0 2012 May 4

  • Caterpillar now pre-compiles, so the coffee-script dependency is no longer needed

v1.0.0 2012 February 11

  • Modularised
  • Added docco docs
  • Debug line is now only outputted if the log level is 7
  • Added setLevel(level)
  • Added History.md
  • Added new screenshots
  • cli-color dependency now accepts revisions

v0.1 2011 September 5

  • Initial commit