npm package: @randomcod3r/logger
Description: A clean, feature-rich logging system for Node.js, browser, and FiveM projects with zero dependencies
Latest version: 1.0.2

# Logger

A clean, feature-rich logging system for Node.js, browser, and FiveM projects with color-coded output, multiple log levels, and timestamps.

## Features

- 🎨 **Color-coded console output** - Easy to read logs with automatic coloring
- 📊 **Multiple log levels** - debug, info, success, warn, error
- ⏰ **Timestamps** - Precise timing for every log entry
- 🔧 **Customizable** - Configure colors, timestamps, prefixes, and more
- 📦 **TypeScript support** - Full TypeScript definitions included
- 🎯 **Child loggers** - Create scoped loggers with prefixes
- 💾 **JSON format** - Optional JSON output for structured logging
- 🌐 **Browser compatible** - Works in Node.js, browser, and FiveM environments

## Installation

```bash
npm install @randomcod3r/logger
```

## Usage

### Basic Usage

```typescript
import { logger } from '@randomcod3r/logger';

logger.debug('Debug information');
logger.info('Application started');
logger.success('Operation completed successfully');
logger.warn('This is a warning');
logger.error('An error occurred', new Error('Something went wrong'));
```

### Using Standalone Functions

```typescript
import { debug, info, success, warn, error } from '@randomcod3r/logger';

debug('Debug message');
info('Info message');
success('Success message');
warn('Warning message');
error('Error message');
```

### Creating a Custom Logger

```typescript
import { Logger } from '@randomcod3r/logger';

const logger = new Logger({
  level: 'info',           // Minimum log level (debug, info, warn, error)
  timestamps: true,        // Show timestamps
  colors: true,            // Enable colors
  prefix: 'MyApp',         // Custom prefix
  json: false,             // Use JSON format
  silent: false,           // Disable console output
});

logger.info('Application initialized');
```

### Child Loggers

Create scoped loggers with additional context:

```typescript
import { Logger } from '@randomcod3r/logger';

const mainLogger = new Logger({ prefix: 'API' });
const dbLogger = mainLogger.child('Database');
const authLogger = mainLogger.child('Auth');

dbLogger.info('Connected to database');    // [API:Database] Connected to database
authLogger.warn('Invalid token attempt');  // [API:Auth] Invalid token attempt
```

### Logging with Additional Data

```typescript
logger.info('User logged in', {
  userId: 123,
  username: 'john_doe',
  ip: '192.168.1.1'
});

logger.error('Request failed', {
  statusCode: 500,
  endpoint: '/api/users',
  duration: 1234
});
```

### JSON Format

```typescript
import { Logger } from '@randomcod3r/logger';

const logger = new Logger({
  json: true
});

logger.info('Structured log entry', { requestId: 'abc-123' });
// Output: {"level":"info","message":"Structured log entry","timestamp":"2023-...","data":{"requestId":"abc-123"}}
```

## Configuration Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `level` | `LogLevel` | `'debug'` | Minimum log level to output |
| `timestamps` | `boolean` | `true` | Enable/disable timestamps |
| `colors` | `boolean` | `true` | Enable/disable colors |
| `prefix` | `string` | `''` | Custom prefix for all logs |
| `json` | `boolean` | `false` | Use JSON format for logs |
| `silent` | `boolean` | `false` | Disable console output |

## Log Levels

The logger supports the following log levels (in order of priority):

1. **debug** - Detailed information for debugging
2. **info** - General informational messages
3. **success** - Success messages
4. **warn** - Warning messages
5. **error** - Error messages

When you set a minimum log level, only logs at that level or higher will be output.

## Examples

### Express.js Integration

```typescript
import express from 'express';
import { Logger } from '@randomcod3r/logger';

const app = express();
const logger = new Logger({ prefix: 'Express' });

app.use((req, res, next) => {
  logger.info(`${req.method} ${req.path}`);
  next();
});

app.listen(3000, () => {
  logger.success('Server started on port 3000');
});
```

### Error Handling

```typescript
import { logger } from '@randomcod3r/logger';

try {
  // Some operation
  throw new Error('Something went wrong');
} catch (err) {
  logger.error('Operation failed', err);
}
```

### Production vs Development

```typescript
import { Logger } from '@randomcod3r/logger';

const logger = new Logger({
  level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
  colors: process.env.NODE_ENV !== 'production',
  json: process.env.NODE_ENV === 'production',
});
```

## License

MIT