json-log-file

    1.2.2 • Public • Published

    json-log-file

    NPM Downloads

    Description

    Log data to file as JSON objects.

    Methods:

    • save(dataToSave: any, logType: string): Promise<string>.

    Installation

    npm install json-log-file --save

    Initialization

    // Import.
    const JsonLogFile = require('json-log-file');
     
    // Init.
    const log = new JsonLogFile('default.log');

    Using

    // Save string.
    log.save('Some string.');
     
    // Save object.
    log.save({ someKey: 'Some value string.' });
     
    // Save string with log type.
    log.save('Some string.', 'some-log-type');
     
    // Save and get log ID.
    const logId = await log.save('Some string.');

    Log record sample

    {
        "logId": "f619d938bd8cdb68",
        "data": "Some string.",
        "type": "some-log-type",
        "timestamp": 1545507631029,
        "timestampIsoString": "2018-12-22T19:40:31.029Z"
    }

    Custom options

    You can initialize logger with custom options. Just define options object in second param.

    Options params:

    • pretty - is pretty JSON (default: true);
    • separator - log records separator string (default: without separator);
    • encoding - data encoding (default: utf8);
    • showInConsole - show log record in console (default: true);
    • useBuffer - use buffer before save to file (default: false);
    • bufferSavingTimeout - buffer saving timeout in milliseconds (default: 100);
    • logIdSize - log ID size (default: 8);
    • logIdEncoding - log ID encoding (default: hex);
    • addLogId - add log ID to log record object (default: true);
    • addTimestamp - add timestamp to log record object (default: true);
    • addTimestampIsoString - add timestamp ISO string to log record object (default: true).
    // Init with custom options. Use buffer with 1 second saving timeout.
    const log = new JsonLogFile('default.log', { useBuffer: true, bufferSavingTimeout: 1000 });

    License

    The MIT License (MIT)

    Copyright © 2018 - 2019 Volodymyr Sichka

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    Install

    npm i json-log-file

    DownloadsWeekly Downloads

    2

    Version

    1.2.2

    License

    MIT

    Unpacked Size

    12.8 kB

    Total Files

    7

    Last publish

    Collaborators

    • volodymyr.sichka