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

@noonesstudios/koa-logger

v2.0.5

Published

A highly customizable with color support and zero dependencies logging utility for koa servers in Nodejs

Downloads

10

Readme

install size

Inspired by

koa-logger morgan

Install

npm i @noonesstudios/koa-logger

Placement

const koa = require('koa');
const app = new koa();
const logger = require('@noonesstudios/koa-logger'); // initialize it once
const errorHandler = require('./path-to-your-error-handler');
const routes = require('./path-to-your-routes');
app.use(errorHandler);
app.use(logger); // place it here. if any error occurs, `logger` logs it and rethrows it up the chain, while exposing ctx.error = error
...
...
app.use(routes);

CONFIG FILE

By default, logger looks for a .js file named koa-logger.config.js in app root. and merges it overridingly, if it exists, with a bunch of defaults, which help it run without any configuration whatsoever.

The function utilizes the native npm command npm root so hopefully you have npm installed =)

You can specify a different filename to look for by appending an argument: --koa-logger-filename=yourfilename.js

API

| Property | Type | Inner Properties | Example | Default | Description | --- | --- | --- | --- | --- | --- | | logDir | string | - | logDir: './logs' | - | Choose where you would like to save any "hard" logs. if the provided directory does not yet exist, logger will create it for you
| log | Object | {errors: boolean} | log: {errors: true} | {} | Events you wish to log to your hard drive. Will only work if logDir is provided. Will not crash otherwise | painter | Function | - | painter: require('chalk') | - | A module to use colors with if you would like to colorize your req/res logs. Hard logs are always set to be saved without them. | customPaths | Object | {anythingYouWant: string / Object: {path: string, str: string, formatter: Function, color: string / Function}} | customPaths: {userId: 'state.id', requestBytes: { path: 'request.length', formatter: v => require('bytes')(v), color: 'red' }} | - | Declare / Override paths which should be found in the ongoing ctx. Once you declare them, you can use them in requestFormat, responseFormat and hardLogFormat
| requestFormat | string | - | requestFormat: --> userId method url anythingYouWant | --> method url ip ua reqLength | Override default request format. You can use custom props if declared in customPaths. Props must be separated by spaces. If a prop was not declared in customPaths, it will be logged as is. e.g url - method will be logged as /users - GET | responseFormat | string | - | responseFormat: <-- status message error anythingYouWant | <-- method url ip ua status - time error message | Same as ^ | hardLogFormat | string | - | hardLogFormat: ip ua url method anythingYouWant error | method url ip ua status - time error | Same as ^, no colors (ansi) attached.

Default Paths

method: 'method',
url: 'originalUrl',
ip: 'ip',
ua: 'headers.user-agent',
status: 'status',
message: 'body.message',
time: 'state.delta',
error: 'error.message'

Example

// koa-logger.config.js or your-custom-filename.js in root dir 

const path = require('path');
const chalk = require('chalk');

module.exports = {
  logDir: path.join(__dirname, './logs'),
  log: {
    errors: true
  },
  painter: chalk,
  customPaths: {
    reqIndicator: {
       str: '-->',
       color: 'gray'
    },
    resIndicator: {
       str: (ctx) => ctx.status < 400 ? '<--' : 'xxx',
       color: (prop, ctx) => ctx.status >= 400 ? 'red' : 'gray'
    },
    userId: {
       path: 'state.user.id',
       formatter: v => v ? v : '[NO_ID]',
       color: 'blue'
    },
  },
  requestFormat: 'reqIndicator userId method url',
  responseFormat: 'resIndicator userId method url - time message error',
  hardLogFormat: 'userId ua method url ::: error'
}

Future

Depends on requests if any.