Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ntananh/tlog
https://github.com/ntananh/tlog
Last synced: 12 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/ntananh/tlog
- Owner: ntananh
- Created: 2024-06-22T17:18:49.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-07-06T07:38:32.000Z (6 months ago)
- Last Synced: 2024-11-07T19:16:03.480Z (2 months ago)
- Language: TypeScript
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# tlog
A simple implementation for logging library.
# Usage
## Basic usage
``` typescript
const logger = new Logger();logger.trace('Trace message');
logger.debug('Debug message');
logger.info('Info message');
logger.warn('Warn message');
logger.error('Error message');logger.info('Log the object', {
foo: 'baz'
});
```## Log Appender
- Appenders serialise log events to some form of output. They can write to files, outputs to console.
- The library provides 2 log appenders:
- **Console Appender** : This appender outputs log messages to the console.
- **File Appender** : This appedner outputs log message to a file (as requirement).
### Logger config parameters
- name (string): The name of the logger.
- timestamps (Boolean): Whether to include timestamps in logs (default: true)
- appenders: Array of appenders to process and log the data. (default: [ConsoleAppender])
### Log Appender Usage
```typescript
// Initialize the console logger
const consoleAppender = new ConsoleAppender();/*
- The file appender will create 'logs/app.log' if it doesn't exist.
- If 'logs/app.log' exists, it will append new logs to the file.
- Log rotation options can be provided to the file appender to automatically
rotate the log file based on date.
*/
const fileAppender = new FileAppender({ path: 'logs/app.log' });// Set up the logger with both console and file appenders
const logger = new Logger({
appenders: [consoleAppender, fileAppender]
});/*
The logger will use both appenders:
- The console appender outputs log messages to the console.
- The file appender writes log messages to 'logs/app.log'.
*/
logger.info('Information log message');
```### Create custom log appender
Can add multiple appenders to a single logger, so that log messages can be sent to multiple outputs.
You can also create custom log appenders by extending the `Appender` class as shown below:
```typescript
export class CustomAppender extends Appender {
constructor() {
super();
}handle(config: AppenderConfig) {
console.log([`[CustomAppender] - ${config.message}`]);
}
}
```
> **Note:** You can also create custom log formatters by implementing the `Formatter` interface.