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

logepi

v1.0.8

Published

A tiny (Togepi-sized) package for logs, built on top of Winston.

Downloads

59

Readme

How to install

Using npm

$ npm install --save logepi

Using yarn

$ yarn install logepi

How to use

The library is comprised of two major components:

  • A Log static class which creates an instance of a Winston logger, exposing several methods to output messages with different loglevels.
  • Middleware functions to add automatic logging of request/response flows for HTTP servers. Currently the library supports Koa and Express.

There's also a TypeScript declaration file available for TS-based apps.

Using the Log API

Importing the library

// commonjs style
const { Log } = require("logepi");

// ES modules style
import { Log } from "logepi";

Log an error

Log.error("An error was found", { tags: { process: "foo" } });

Log a warning

Log.warning("Careful!", { tags: { process: "foo" } });

Log at INFO level

Log.info("Hopefully someone will read this.", { tags: { process: "foo" } });

Log at VERBOSE level

Log.verbose("Now I'm all blah-by.", { tags: { process: "foo" } });

Log at DEBUG log

Log.debug("I've said too much!", { tags: { process: "foo" } });

Setting up an output level

In order to filter what messages are printed to the console transport, you can use the Log.setOutputLevel() method:

// Limit messages to "error" level.
Log.setOutputLevel("error");

You can also use the LogLevel enum, with uppercased keys (INFO, WARN, ERROR, VERBOSE, DEBUG).

Using the Koa middleware

Import the library

// commonjs style
const { KoaLoggingMiddleware } = require("logepi");

// ES modules style
import { KoaLoggingMiddleware } from "logepi";

Inject it into your app

app
    .use(...)
    // ...make sure it's the *last* middleware...
    .use(KoaLoggingMiddleware());

Using the Express middleware

Import the library

// commonjs style
const { ExpressLoggingMiddleware } = require("logepi");

// ES modules style
import { ExpressLoggingMiddleware } from "logepi";

Inject it into your app

app
    .use(...)
    // ...make sure it's the *last* middleware...
    .use(ExpressLoggingMiddleware());

Adding tags to logs

Logepi offers a way to inject tagged data in a logline, with the format [key:value]. When logging, use the tags key on the data argument of any of the Log API methods. There's an example right in the next section.

Configuring logging middlewares

All middlewares expose the same configuration object:

  /**
   * Allows to configure log levels for requests and responses.
   */
  logLevels: {
    /**
     * Sets the log level for all incoming requests.
     */
    request: "info",

    /**
     * Sets the log level for all outgoing responses, according to their status code.
     */
    response: {
      status2xx: "info",  // For 200-399 codes
      status4xx: "warn",  // For 400-499 codes
      status5xx: "error"  // For 500-599 codes
    }
  }

How does it look?

Note: Logs are single-lined, but for clarity's sake, they're shown in multiple lines.

Manual logging

Log.info("API is now ready", { tags: { port } });

outputs:

[timestamp:2019-01-11T21:04:41.915Z]
[level:info]
[message:API is now ready]
[port:9000]

Incoming request

[timestamp:2019-01-11T21:00:45.442Z]
[level:info]
[message:IncomingRequest]
[url:/api/apps]
[queryParams:undefined]
[body:{}]
[headers:{"host":"localhost:9000","connection":"keep-alive","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","accept-encoding":"gzip, deflate, br","accept-language":"es-ES,es;q=0.9,en;q=0.8,en-US;q=0.7"}]

Outgoing response

[timestamp:2019-01-11T21:00:45.446Z]
[level:info]
[message:OutgoingResponse]
[status:200]
[body:{"ok":true,"data":{"foo":true}}]
[headers:{"host":"localhost:9000","connection":"keep-alive","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8","accept-encoding":"gzip, deflate, br","accept-language":"es-ES,es;q=0.9,en;q=0.8,en-US;q=0.7"}]

Issues

Feel free to report any issues and we'll figure out a way to make it work.

Want to contribute?

That's great! Open an issue first and let's discuss what you want to add or, by all means, fork this project! :)

License

This project is licensed under the MIT License.