https://github.com/gavinbirkhoff/swiftlet-log
Logging utility with different log levels with timestamp options.
https://github.com/gavinbirkhoff/swiftlet-log
Last synced: about 1 year ago
JSON representation
Logging utility with different log levels with timestamp options.
- Host: GitHub
- URL: https://github.com/gavinbirkhoff/swiftlet-log
- Owner: GavinBirkhoff
- License: mit
- Created: 2024-01-14T15:21:01.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-10T08:21:42.000Z (over 2 years ago)
- Last Synced: 2025-01-25T05:14:22.188Z (over 1 year ago)
- Language: TypeScript
- Size: 141 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swiftlet-log
Logging utility with different log levels with timestamp options.
## Usage
1. **Installation**
Install the Logger package using npm:
```bash
npm install swiftlet-log
```
Install the Logger package using yarn:
```bash
yarn add swiftlet-log
```
Install the Logger package using pnpm:
```bash
pnpm add swiftlet-log
```
2. **Import and Create Logger Instance**
Import the Logger class and create an instance:
```typescript
import { Logger, LogLevel } from 'swiftlet-log'
const logger = new Logger({ level: LogLevel.DEBUG, timestamp: true })
```
3. **Log Messages**
Use the different log level methods to log messages:
```typescript
logger.debug('This is a debug message.')
logger.info('This is an info message.')
logger.warning('This is a warning message.')
logger.error('This is an error message.')
logger.trace('This is a trace message.')
logger.fatal('This is a fatal message.')
```
4. **Middleware**
```typescript
import { Logger, LogLevel, LogMiddleware } from 'swiftlet-log'
const logger = new Logger({ level: LogLevel.DEBUG, timestamp: true })
const customPrefixMiddleware: LogMiddleware = (ctx, next) => {
ctx.message = `[CUSTOM PREFIX] ${ctx.message}`
next(ctx.message, ctx.level)
}
logger.use(customPrefixMiddleware)
logger.info('This is an info message.') // 输出: [CUSTOM PREFIX] [INFO]: This is an info message.
```
5. **Customization**
- Adjust log level:
```typescript
logger.setLogLevel(LogLevel.INFO)
```
- Enable or disable timestamp:
```typescript
logger.enableTimestamp()
logger.disableTimestamp()
```
- Add or remove log listeners:
```typescript
const customListener: LogListener = (level, message) => {
// Your custom log listener logic
}
logger.addLogListener(customListener)
logger.removeLogListener(customListener)
```
## Log Levels
- `DEBUG`: Detailed debugging information.
- `INFO`: General information about system operation.
- `WARNING`: Indicates a potential problem.
- `ERROR`: Indicates a more serious problem.
- `TRACE`: Very detailed tracing information.
- `FATAL`: A very severe error that may lead to application termination.