• Stars
    star
    266
  • Rank 154,103 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 7 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Extremely fast utf8 only stream implementation

sonic-boom

NPM Package Version Build Status js-standard-style

Extremely fast utf8-only stream implementation to write to files and file descriptors.

This implementation is partial, but support backpressure and .pipe() in is here. However, it is 2-3x faster than Node Core fs.createWriteStream():

benchSonic*1000: 1916.904ms
benchSonicSync*1000: 8605.265ms
benchSonic4k*1000: 1965.231ms
benchSonicSync4k*1000: 1588.224ms
benchCore*1000: 5851.959ms
benchConsole*1000: 7605.713ms

Note that sync mode without buffering is slower than a Node Core WritableStream, however this mode matches the expected behavior of console.log().

Note that if this is used to log to a windows terminal (cmd.exe or powershell), it is needed to run chcp 65001 in the terminal to correctly display utf-8 characters, see chcp for more details.

Install

npm i sonic-boom

Example

'use strict'

const SonicBoom = require('sonic-boom')
const sonic = new SonicBoom({ fd: process.stdout.fd }) // or { dest: '/path/to/destination' }

for (let i = 0; i < 10; i++) {
  sonic.write('hello sonic\n')
}

API

SonicBoom(opts)

Creates a new instance of SonicBoom.

The options are:

  • fd: a file descriptor, something that is returned by fs.open or fs.openSync.
  • dest: a string that is a path to a file to be written to (mode controlled by the append option).
  • minLength: the minimum length of the internal buffer that is required to be full before flushing.
  • maxLength: the maximum length of the internal buffer. If a write operation would cause the buffer to exceed maxLength, the data written is dropped and a drop event is emitted with the dropped data
  • maxWrite: the maximum number of bytes that can be written; default: 16384
  • sync: perform writes synchronously (similar to console.log).
  • fsync: perform a fsyncSync every time a write is completed.
  • append: appends writes to dest file instead of truncating it (default true).
  • mode: specify the creating file mode (see fs.open() from Node.js core).
  • mkdir: ensure directory for dest file exists when true (default false).
  • retryEAGAIN(err, writeBufferLen, remainingBufferLen): a function that will be called when sonic-boom write/writeSync/flushSync encounters a EAGAIN or EBUSY error. If the return value is true sonic-boom will retry the operation, otherwise it will bubble the error. err is the error that caused this function to be called, writeBufferLen is the length of the buffer sonic-boom tried to write, and remainingBufferLen is the length of the remaining buffer sonic-boom didn't try to write.

For sync:false a SonicBoom instance will emit the 'ready' event when a file descriptor is available. For sync:true this is not relevant because the 'ready' event will be fired when the SonicBoom instance is created, before it can be subscribed to.

SonicBoom#write(string)

Writes the string to the file. It will return false to signal the producer to slow down.

SonicBoom#flush()

Writes the current buffer to the file if a write was not in progress. Do nothing if minLengthΒ is zero or if it is already writing.

SonicBoom#reopen([file])

Reopen the file in place, useful for log rotation.

Example:

const stream = new SonicBoom('./my.log')
process.on('SIGUSR2', function () {
  stream.reopen()
})

SonicBoom#flushSync()

Flushes the buffered data synchronously. This is a costly operation.

SonicBoom#end()

Closes the stream, the data will be flushed down asynchronously

SonicBoom#destroy()

Closes the stream immediately, the data is not flushed.

Events

SonicBoom#close

See Stream#close. The 'close' event when the instance has been closed.

SonicBoom#drain

See Stream#drain. The 'drain' event is emitted when source can resume sending data.

SonicBoom#drop

When destination file maximal length is reached, the 'drop' event is emitted with data that could not be written.

SonicBoom#error

The 'error' event is emitted when the destination file can not be opened, or written.

SonicBoom#finish

See Stream#finish. The 'finish' event after calling end() method and when all data was written.

SonicBoom#ready

The 'ready' event occurs when the created instance is ready to process input.

SonicBoom#write

The 'write' event occurs every time data is written to the underlying file. It emits the number of written bytes.

License

MIT

More Repositories

1

pino

🌲 super fast, all natural json logger
JavaScript
14,160
star
2

pino-pretty

🌲Basic prettifier for Pino log lines
JavaScript
1,212
star
3

pino-http

🌲 high-speed HTTP logger for Node.js
JavaScript
535
star
4

thread-stream

A streaming way to send data to a Node.js Worker Thread
JavaScript
229
star
5

express-pino-logger

🌲 an express middleware to log with pino
JavaScript
199
star
6

pino-elasticsearch

🌲 load pino logs into Elasticsearch
JavaScript
176
star
7

pino-debug

🌲high performance debug logging 🐞
JavaScript
147
star
8

koa-pino-logger

🌲 pino logging koa middleware
JavaScript
91
star
9

pino-tee

🌲 tee pino logs into a file, with multiple levels
JavaScript
89
star
10

pino-multi-stream

🌲 A wrapper for Pino to provide Bunyan's multiple stream API
JavaScript
69
star
11

pino-nextjs-example

JavaScript
66
star
12

pino-noir

🌲 pino log redaction 🍷
JavaScript
66
star
13

pino-std-serializers

🌲 A list of standard object serializers for the Pino logger
JavaScript
58
star
14

pino-caller

🌲 Include call site of pino log messages
JavaScript
56
star
15

pino-mongodb

🌲 Insert JSON from stdin into MongoDB
JavaScript
54
star
16

pino-socket

🌲 A transport for sending pino logs to network sockets
JavaScript
42
star
17

pino-abstract-transport

Write Pino transports easily
JavaScript
34
star
18

pino-syslog

🌲 A transport for reformatting pino logs into standard syslog format
JavaScript
29
star
19

pino-arborsculpture

🌲 Change Pino log levels in a running process
JavaScript
24
star
20

pino-opentelemetry-transport

OpenTelemetry transport for Pino
JavaScript
21
star
21

pino-webpack-plugin

JavaScript
19
star
22

quick-format-unescaped

Solves a problem with util.format
JavaScript
17
star
23

pino-gelf

🌲 Convert Pino logs to GELF format and send to Graylog
JavaScript
13
star
24

restify-pino-logger

🌲 pino logging restify middleware
JavaScript
12
star
25

pino-inspector

Send your pino logs to the node inspector!
JavaScript
12
star
26

pino-http-print

🌲 debug HTTP printer for pino
JavaScript
9
star
27

pino-filter

🌲 A transport to filter log lines in the manner of the `debug` module
JavaScript
9
star
28

real-require

Keep require and import consistent after bundling or transpiling.
JavaScript
8
star
29

pino-clf

🌲 Transport which transforms Pino HTTP logs into Common Log Format
JavaScript
5
star
30

getpino.io

The website for pino
CSS
5
star
31

pino-toke

🌲 Transform Pino HTTP log messages with a format string
JavaScript
5
star
32

pino-test

JavaScript
2
star
33

pino-sapling

🌲 seed template for creating pino plugins/libraries/transports/utilities/modules
JavaScript
2
star
34

community

🌲 data on the members of the pino community
JavaScript
1
star
35

rill-pino-logger

🌲pino logging rill middleware
JavaScript
1
star