Skip to main content

TypeScript Support

Logluxe is written in TypeScript and provides complete type definitions out of the box.

Zero Configuration

TypeScript types are automatically available:

import { log, createLogger } from 'logluxe';

// Full autocomplete and type checking
log.success('Typed logging!');
log.error('Error message', new Error('Oops'));
log.info('Info with data', { key: 'value' });
TypeScript Output
✔ Typed logging!
✖ Error message Error: Oops
ℹ Info with data { key: 'value' }

Type Definitions

Logger Instance

import type { Logger } from 'logluxe';

const myLogger: Logger = createLogger({
prefix: 'App'
});

// All methods are fully typed
myLogger.success('message');
myLogger.error('message');
myLogger.warn('message');
myLogger.info('message');
myLogger.debug('message');

Configuration Types

import type { LoggerConfig, LogLevel, Theme } from 'logluxe';

const config: LoggerConfig = {
level: 'info', // Type-checked: 'debug' | 'info' | 'warn' | 'error' | 'silent'
colors: true, // boolean | 'auto'
timestamp: true, // boolean | TimestampConfig
prefix: 'MyApp', // string | PrefixConfig
theme: 'dracula', // string | Theme
icons: { // IconConfig
success: '✓',
error: '✗',
warning: '⚠',
info: 'ℹ',
debug: '·'
}
};

const log = createLogger(config);
Type-Checked Configuration
// TypeScript catches errors at compile time:
✖ Type '"verbose"' is not assignable to type 'LogLevel'
✔ Valid levels: 'debug' | 'info' | 'warn' | 'error' | 'silent'

Theme Types

import type { Theme, StyleOptions } from 'logluxe';

const customTheme: Theme = {
name: 'my-theme',
colors: {
success: '#00ff00',
error: '#ff0000',
warning: '#ffff00',
info: '#00ffff',
debug: '#888888'
},
styles: {
success: { bold: true },
error: { bold: true, underline: true },
warning: { bold: true },
info: {},
debug: { dim: true }
},
icons: {
success: '✓',
error: '✗',
warning: '⚠',
info: 'ℹ',
debug: '·'
}
};

Style Builder Types

import type { StyleBuilder } from 'logluxe';

const builder: StyleBuilder = log.paint('Hello');

// All methods return StyleBuilder for chaining
builder
.red()
.bold()
.underline()
.print();

// toString() returns string
const styled: string = builder.toString();
🎮 Try Style Builder

Generic Logger

Typed Context

interface AppContext {
requestId: string;
userId?: string;
service: string;
}

const log = createLogger<AppContext>({
context: {
requestId: '123',
service: 'api'
}
});

// Context is type-checked
log.withContext({
userId: 'user123' // Must match AppContext
});
Typed Context
// TypeScript ensures context matches interface:
✔ log.withContext({ userId: 'user123' }) // OK
✖ log.withContext({ invalid: 'key' }) // Error!

Typed Child Loggers

interface ServiceContext {
service: string;
version: string;
}

const rootLog = createLogger<ServiceContext>({
context: { service: 'main', version: '1.0.0' }
});

// Child inherits context type
const childLog = rootLog.child({
prefix: 'Database'
});

Type Guards

Environment Detection

import { env } from 'logluxe';

// Type narrowing based on environment
if (env.isNode) {
// Node.js specific code
process.stdout.write('Node.js\n');
}

if (env.isBrowser) {
// Browser specific code
document.title = 'Browser';
}

Error Handling

function handleError(error: unknown) {
// Type guard for Error
if (error instanceof Error) {
log.error(error.message);
log.debug(error.stack);
} else {
log.error(String(error));
}
}

Strict Mode

Logluxe works great with strict TypeScript settings:

// tsconfig.json
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitReturns": true
}
}

Type-Safe Utilities

Typed Color Functions

import type { Color, ColorName } from 'logluxe';

// Named colors are type-checked
const color1: ColorName = 'red'; // ✓
const color2: ColorName = 'invalid'; // ✗ Error

// Color type accepts all formats
const hex: Color = '#ff0000';
const rgb: Color = 'rgb(255, 0, 0)';
const named: Color = 'red';
Color Types
✔ const color: ColorName = 'red' // OK
✔ const color: Color = '#ff0000' // OK
✖ const color: ColorName = 'invalid' // Error!

Typed Log Levels

import type { LogLevel } from 'logluxe';

function setLogLevel(level: LogLevel) {
log.configure({ level });
}

setLogLevel('debug'); // ✓
setLogLevel('info'); // ✓
setLogLevel('warn'); // ✓
setLogLevel('error'); // ✓
setLogLevel('silent'); // ✓
setLogLevel('invalid'); // ✗ Error

IDE Integration

VS Code

Logluxe provides excellent VS Code integration:

  • IntelliSense: Full autocomplete for all methods
  • Hover Information: Type information on hover
  • Go to Definition: Jump to type definitions
  • Error Highlighting: Immediate feedback on type errors

JetBrains IDEs

Works great with WebStorm, IntelliJ, and other JetBrains IDEs:

  • Full TypeScript support
  • Automatic imports
  • Type inference

Complete Application Example

import { 
createLogger,
Logger,
LoggerConfig,
Theme,
env
} from 'logluxe';

// Define application context type
interface AppContext {
service: string;
version: string;
environment: 'development' | 'staging' | 'production';
}

// Define custom theme
const appTheme: Theme = {
name: 'app',
colors: {
success: '#10b981',
error: '#ef4444',
warning: '#f59e0b',
info: '#3b82f6',
debug: '#6b7280'
},
styles: {
success: { bold: true },
error: { bold: true },
warning: {},
info: {},
debug: { dim: true }
},
icons: {
success: '✓',
error: '✗',
warning: '⚠',
info: 'ℹ',
debug: '·'
}
};

// Create typed configuration
const config: LoggerConfig = {
theme: appTheme,
timestamp: true,
level: env.isDev ? 'debug' : 'info',
context: {
service: 'my-api',
version: '1.0.0',
environment: process.env.NODE_ENV as AppContext['environment']
}
};

// Create typed logger
const log: Logger = createLogger(config);

// Export for use across application
export { log };
export type { AppContext };
Application Logger Output
[2024-01-15T10:30:45.123Z]
✓ Server started
[2024-01-15T10:30:45.456Z]
ℹ Environment: development
[2024-01-15T10:30:45.789Z]
· Debug mode enabled
🎮 Try Typed Logger

Migration from JavaScript

If you're migrating from JavaScript:

// Before (JavaScript)
const log = require('logluxe');
log.info('Hello');

// After (TypeScript)
import { log } from 'logluxe';
log.info('Hello'); // Fully typed!

Types are included in the package, no need for @types/logluxe.


Next: Migration Guide