# @mutoe/koam-logger

> Logger middleware for Koam

Latest version **2.3.1** (published 2024-07-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @mutoe/koam-logger
pnpm add @mutoe/koam-logger
yarn add @mutoe/koam-logger
bun add @mutoe/koam-logger
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.3.1 |
| Published | 2024-07-19 |
| First published | 2023-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >= 14 |
| Dependencies | 0 |
| Unpacked size | 30.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | mutoe |
| Maintainers | mutoe |
| Keywords | koa, koam, http-server, logger |

## Links

- npm: https://www.npmjs.com/package/@mutoe/koam-logger
- Repository: https://github.com/mutoe/koam
- Homepage: https://github.com/mutoe/koam/tree/main/packages/koam-logger
- Issues: https://github.com/mutoe/koam/issues
- npm.io page: https://npm.io/package/@mutoe/koam-logger

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 2.3.1 (latest) — 2024-07-19
- 2.2.0 — 2024-01-15
- 2.1.0 — 2023-08-08

## README

# Koam-logger

Implement a simple Koa logger.

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

## Usage

```ts
// 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
   ```ts
   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
   ```ts
   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
   ```ts
   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
   ```ts
   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)
   ```ts
   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.

---
_Source: https://npm.io/package/@mutoe/koam-logger · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
