coak
v1.0.1
Published
simple logger with stream support.
Readme
🌴 coak
📥 Install
npm install coak🚀 Usage
import coak from "coak";
import { createWriteStream } from "fs";
const stream = createWriteStream('log.json');
const logger = coak({ label: 'your-label', stream });
logger.info('Hello from coak!');
logger.end(); // <- end stream;📓 Documentation
coak(opts)
coak is the default export. It accepts an optional configuration object:
label: Adds a custom label to each log entry;stream: A writable stream where logs will be written in JSON format;
import coak from "coak";
const logger = coak();Logger
The logger object returned by coak() exposes the following methods:
info(...data)- Creates an info-level log (level 0);warn(...data)- Creates a warning-log (level 1);error(...data)- Creates a error-log (level 2);write(data, level)(stream-only) - Writes a log directly to the JSON stream;end()(stream-only) - Finalizes the JSON and ends the writable stream;
🍪 Recipes
End stream on process exit
To ensure your stream ends properly and your JSON log is fully written when the process exits, use process.on() to handle exit signals:
import coak from "coak";
import { createWriteStream } from "fs";
const stream = createWriteStream('log.json');
const logger = coak({ label: 'your-label', stream });
logger.info('Hello from coak!');
const exit = () => {
logger.end();
process.exit();
};
process.on('exit', exit);
process.on('SIGINT', exit); // <- CTRL + C📜 Licenses
- coak - MIT
- typescript - Apache-2.0
