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

@mutoe/koam-logger

v2.3.1

Published

Logger middleware for Koam

Downloads

9

Readme

Koam-logger

Implement a simple Koa logger.

THIS FRAMEWORK HAVE NOT BEEN STRICTLY TESTED, PLEASE DO NOT USE IT IN PRODUCTION ! 许多功能未经严格测试,请勿用于生产目的!

Usage

// entrypoint
import Koa, { logger } from '@mutoe/koam'

const app = new Koa()

app.use(logger({
  moduleName: 'My App',
  drivers: [
    {
      type: 'console',
    },
    {
      type: 'file',
      dir: '/tmp/my-app',
      filename: 'my-app.log',
      errorFilename: 'my-app-error.log'
    },
  ]
}))

// in anywhere
app.use(ctx => {
  ctx.log('Hello, Koam!') // -> echo '[LOG] [My App] Hello, Koam! {"method":"GET",status:404, ...}'
  ctx.status = 200
  ctx.log.error('Hello, Koam!') // -> echo '[ERROR] [My App] Hello, Koam! {"method":"GET",status:200,...}'
})

// And you can view the file in `/tmp/my-app/my-app.log`
// 2023-08-06T20:28:56.221+08:00 [LOG] [My App] Hello, Koam! {"method":"GET","url":"/","status":404}
// 2023-08-06T20:28:56.221+08:00 [ERROR] [My App] Hello, Koam! {"method":"GET","url":"/","status":200}

Feature

  1. Native STDOUT/STDERR output in the console

    import { Logger } from '@mutoe/koam'
    const logger = new Logger({
      drivers: [
        { type: 'console' },
      ]
    })
  2. Write logs to files for logger platform like ELK or Grafana Loki

    import { Logger } from '@mutoe/koam'
    const logger = new Logger({
      drivers: [
        {
          type: 'file',
          dir: '/tmp/my-app',
          // optional, default is `output.log`
          filename: 'my-app.log',
          // optional, default is `output.log`
          errorFilename: 'my-app-error.log',
        },
      ]
    })
  3. Timestamp when the log is written

    import { Logger } from '@mutoe/koam'
    const logger = new Logger({
      // your custom timestamp format, default is ISO-8601 format
      datetimeFormat: 'YYYY-MM-DDTHH:mm:ss.SSSZ',
    })

    It will auto set when using file driver

  4. Use it as standalone logger utils

    import { Logger } from '@mutoe/koam-logger'
    const logger = new Logger()
    
    logger.debug('Hello')
  5. If you're using logger as Koa middleware, it will automatically log the request or response information (by customRequestLogger option in logger middleware)

    import { logger } from '@mutoe/koam-logger'
    const app = new Koa()
    app.use(logger({
      customRequestLogger: ctx => ({
        method: ctx.method,
        url: ctx.url,
        traceId: ctx.traceId,
        status: ctx.status,
        responseTime: ctx.responseTime,
      })
    }))

    NOTICE: It should be noted that the content of the context depends on when you call the log method. For example, if you haven't got a response when you call the log, then the response won't fetch the content.