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

sfn-logger

v0.5.4

Published

Simple Friendly Node.js Logger.

Downloads

15

Readme

SFN-Logger

Simple and friendly NodeJS file logger.

This documentation is for sfn-logger 0.4.x, old versions are deprecated.

Install

npm install sfn-logger --save

Example

const Logger = require("sfn-logger");

var logger = new Logger("example.log");

logger.log("Hello, World!");

Create Logger

  • new Logger(filename: string) Creates a logger instance with a filename.
  • new Logger(options: Logger.Options) Creates a logger instance with options.
  • Logger.Options
    • ttl?: number How much time should the output buffer keep contents before flushing, default value is 1000 ms.
    • size?: number How much size should the output buffer keep contents before flushing. This option conflicts with ttl, set only one of them. For data integrity, the real size of flushing data may be smaller than the setting value.
    • filename: string Writes the contents to the target file.
    • fileSize?: number The size of the log file, when up to limit, logs will be compressed or sent via e-mail, default value is 2097152 bytes (2 Mb). For data integrity, the real size of the file may be smaller than the setting value.
    • dateFormat?: string The format of prefix date-time value, default value is YYYY-MM-DDTHH:mm:ss, used by moment.
    • trace?: boolean The log message should contain the filename and position of where triggers the logging operation, false by default.
    • toConsole?: boolean The log should also be output to the console, false by default.
    • outputLevel?: number Sets the minimum level of logs that should be output to the file, default value is Logger.Levels.DEBUG.
    • mail An object configures a new Mail instance (from sfn-mail) or a existing Mail instance.
  • Logger.Levels An enum object contains number from 1 - 4
    • DEBUG
    • INFO
    • WARN
    • ERROR
// Simplest way to create a logger:
var logger = new Logger("example.log");

// Or specify more details:
var logger = new Logger({
    size: 1024 * 4,
    filename: "example.log",
    fileSize: 1024 * 1024 * 2,
});

// Send log file contents as an email when the file size up to limit:
var logger = new Logger({
    size: 1024 * 4,
    filename: "example.log",
    fileSize: 1024 * 1024 * 2,
    mail: { // must set all neccesary properties.
        subject: "Logs of My Website"
        host: "smtp.mail.qq.com",
        port: 25,
        from: "[email protected]",
        to: "[email protected]",
        auth: {
            username: "[email protected]",
            password: "xxxxxxxx",
        }
    }
});

If you don't set the mail option, when file's size up to limit, its contents will be compressed to a GZip file and stored in a directory named according to date.

Familiar Methods

  • logger.log() Logs a message on DEBUG level (alias logger.debug()).
  • logger.info() Logs a message on INFO level.
  • logger.warn() Logs a message on WARN level.
  • logger.error() Logs a message on ERROR level.

These methods' usage are exactly the same as console's, if you're not familiar with them, please check https://nodejs.org/dist/latest-v8.x/docs/api/console.html.

Multi-Processing Scenario

Powered by open-channel, this package is safe in multi-processing scenario, and automatically prevent concurrency conflicts.

Close Logger

You can called the method logger.close() to close the logger, however, due to using open-channel, which serves an internal IPC server that cannot be closed, the program will not be able to exit automatically as usual, you have to explicitly calling process.exit() in case to terminate the program.

// close the logger and terminate the program.
logger.close(() => {
    process.exit();
});