Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/layouwen/logger
A logger for Node.js
https://github.com/layouwen/logger
Last synced: 2 days ago
JSON representation
A logger for Node.js
- Host: GitHub
- URL: https://github.com/layouwen/logger
- Owner: Layouwen
- Created: 2024-08-08T09:02:20.000Z (3 months ago)
- Default Branch: master
- Last Pushed: 2024-10-10T03:00:47.000Z (about 1 month ago)
- Last Synced: 2024-10-10T03:09:24.168Z (about 1 month ago)
- Language: TypeScript
- Size: 14.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Logger
A logger for Node.js.
[![Version npm](https://img.shields.io/npm/v/@avanlan/logger.svg?style=flat-square)](https://www.npmjs.com/package/@avanlan/logger)
[![NPM Downloads](https://img.shields.io/npm/dw/%40avanlan%2Flogger)](https://www.npmjs.com/package/@avanlan/logger)[![NPM](https://nodei.co/npm/@avanlan/logger.png?downloads=true&downloadRank=true)](https://nodei.co/npm/@avanlan/logger/)
## Install
```bash
pnpm i @avanlan/logger
```## Usage
```ts
import { Logger } from '@avanlan/logger';const logger = new Logger();
logger.access.info("access log")
logger.daily.info("daily log")
logger.error.error("error log")
logger.debug.info("debug log")// output
// [2024-08-08 16:37:24] [main-app] [INFO]: access log
// [2024-08-08 16:37:24] [main-app] [INFO]: daily log
// [2024-08-08 16:37:24] [main-app] [ERROR]: error log
// [2024-08-08 16:37:24] [main-app] [INFO]: debug log
```## Configuration
### Project
Specify the project name.
```ts
import { Logger } from '@avanlan/logger';const logger = new Logger({
projectName: "auth-service",
});logger.daily.info("info log")
// output daily log
// [2024-08-08 16:37:24] [auth-service] [INFO]: info log
```## Output Logger File
```
project
├── logger
│ ├── access
│ │ └── access.YYYY-MM-DD.log
│ ├── daily
│ │ └── daily.YYYY-MM-DD.log
│ ├── error
│ │ └── error.YYYY-MM-DD.log
│ └── debug.log
```## Logger Middleware
### Koa
```ts
import { Logger, koaHttpLogger } from "@avanlan/logger";
import { bodyParser } from '@koa/bodyparser';const logger = new Logger();
app.use(bodyparser());
app.use(koaHttpLogger(logger));// output access log
// [2024-08-08 17:34:20] [main-app] [INFO]: 2ms GET / ::1 {"headers":{"host":"localhost:8044","user-agent":"curl/8.6.0","accept":"*/*"},"query":{},"body":{}}
```### Express
```ts
import { Logger, expressHttpLogger } from "@avanlan/logger";
import bodyParser from 'body-parser';const logger = new Logger();
app.use(bodyParser.json());
app.use(expressHttpLogger(logger));// output access log
// [2024-08-08 17:47:55] [main-app] [INFO]: 0ms GET / ::1 {"headers":{"host":"localhost:5834","user-agent":"curl/8.6.0","accept":"*/*"},"query":{}}
```