Skip to main content

@ttoss/logger

Send notifications to Discord, Slack, or any custom endpoint from your applications.

Installation

pnpm add @ttoss/logger

Quick Start

import { configureLogger, notify, log } from '@ttoss/logger';

// Configure once at app startup
configureLogger({
project: 'My App',
discordWebhookUrl: 'https://discord.com/api/webhooks/xxx',
});

// Send notifications
await notify({ type: 'error', message: 'Something went wrong!' });
await notify({ type: 'info', title: 'Deploy', message: 'v1.2.0 released' });

// Local console logging
log.info('Server started');
log.warn('High memory usage');
log.error('Database connection failed');

Custom Endpoints

Send notifications to any platform by providing a custom formatter:

configureLogger({
project: 'My App',
discordWebhookUrl: 'https://discord.com/api/webhooks/xxx',
customEndpoints: [
{
url: 'https://hooks.slack.com/services/xxx',
formatBody: ({ notification, project }) => ({
text: `[${project}] ${notification.type}: ${notification.message}`,
}),
},
{
url: 'https://my-api.com/alerts',
headers: { Authorization: 'Bearer token' },
method: 'PUT',
formatBody: ({ notification }) => ({
severity: notification.type,
message: notification.message,
}),
},
],
});

CustomEndpoint Options

PropertyTypeDescription
urlstringEndpoint URL (required)
formatBodyfunctionFormats the request body (required)
headersRecord<string, string>Custom headers (default: { 'Content-Type': 'application/json' })
method'POST' | 'PUT' | 'PATCH'HTTP method (default: 'POST')
namestringOptional identifier for debugging

API

configureLogger(config)

ParameterTypeDescription
projectstringProject name for notification context
discordWebhookUrlstringDiscord webhook URL
customEndpointsCustomEndpoint | CustomEndpoint[]Custom notification endpoints

notify(notification)

ParameterTypeDescription
type'info' | 'warn' | 'error'Notification severity
messagestringNotification content
titlestringOptional title
logbooleanAlso log to console

notifyError({ error, title?, log? })

Convenience method for error notifications that extracts the message from Error objects.

log

Console logging methods: log.info(), log.warn(), log.error()

License

MIT