86 lines
1.8 KiB
Markdown
86 lines
1.8 KiB
Markdown
# logger
|
|
|
|
A lightweight console and file logger with color-coded output and timestamped entries. Automatically includes the calling file and supports hourly log separation in saved files.
|
|
|
|
## Features
|
|
|
|
- Singleton logger instance
|
|
- Colored console output
|
|
- Log to file with date-based filenames
|
|
- Hourly separators in saved logs
|
|
- TypeScript-friendly with exported types
|
|
- Supports `info`, `warn`, `error`, and `custom` log levels
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
npm install @creations.works/logger
|
|
```
|
|
|
|
Or with Bun:
|
|
|
|
```bash
|
|
bun add @creations.works/logger
|
|
```
|
|
|
|
## Usage
|
|
|
|
```ts
|
|
import { logger } from "@creations.works/logger";
|
|
|
|
logger.info("This is an info message");
|
|
logger.warn("This is a warning", { breakLine: true });
|
|
logger.error(new Error("Something went wrong"), { save: true });
|
|
|
|
logger.custom("[DEBUG]", "main.ts", "Detailed debug message", "34", {
|
|
save: true,
|
|
breakLine: true,
|
|
});
|
|
```
|
|
|
|
## API
|
|
|
|
### logger.info(message, options?)
|
|
|
|
Log an informational message.
|
|
|
|
### logger.warn(message, options?)
|
|
|
|
Log a warning message.
|
|
|
|
### logger.error(message, options?)
|
|
|
|
Log an error message or exception.
|
|
|
|
### logger.custom(label, source, message, color, options?)
|
|
|
|
Log a custom message with a custom tag, source, and ANSI color code.
|
|
|
|
### logger.space()
|
|
|
|
Prints an empty line to the console.
|
|
|
|
## Options
|
|
|
|
All logging methods accept an optional object:
|
|
|
|
- `breakLine` (boolean): whether to add a newline after the message in the console
|
|
- `save` (boolean): whether to write the message to the file log
|
|
/
|
|
## Types
|
|
|
|
```ts
|
|
type ILogMessagePart = { value: string; color: string };
|
|
|
|
type ILogMessageParts = {
|
|
level: ILogMessagePart;
|
|
filename: ILogMessagePart;
|
|
readableTimestamp: ILogMessagePart;
|
|
message: ILogMessagePart;
|
|
[key: string]: ILogMessagePart;
|
|
};
|
|
```
|
|
|
|
## License
|
|
|
|
[BSD 3-Clause](LICENSE)
|