The official JavaScript SDK for DuckBug.io - a flexible logging and error tracking platform.
- π¦ Simple Integration: Easy setup with DuckBug.io
- π Provider Architecture: Extensible plugin system for custom logging providers
- π Multiple Log Levels: Support for debug, info, warn, and error levels
- π― TypeScript Support: Full TypeScript support with type definitions
- π¦ Dual Module Format: Both CommonJS and ES Module support
- β‘ Lightweight: Minimal dependencies and small bundle size
# npm
npm install @duckbug/js
# yarn
yarn add @duckbug/js
# pnpm
pnpm add @duckbug/js
import { DuckSDK, DuckBugProvider } from '@duckbug/js';
// Initialize with DuckBug.io provider
const providers = [
new DuckBugProvider({
dsn: 'your-duckbug-dsn-here'
})
];
// Create SDK instance with optional configuration
const duck = new DuckSDK(providers, {
logReports: {
log: false,
warn: true,
error: true,
}
});
// Start logging
duck.log('Info message', { userId: 123, action: 'user_login' });
duck.debug('Debug message', { debugInfo: 'Connection established' });
duck.warn('Warning message', { warning: 'Rate limit approaching' });
duck.error('Error message', { error: 'Database connection failed' });
duck.fatal('Fatal message', { error: 'Ay, caramba' });
//Send error
const testError = new Error("Integration test error");
testError.stack =
"Error: Integration test error\n at integration.test.ts:1:1";
// Use quack method directly on provider
duckBugProvider.quack("INTEGRATION_ERROR", testError);
The main SDK class that manages logging across multiple providers.
new DuckSDK(providers: Provider[], config?: LogProviderConfig)
providers
: Array of provider instancesconfig
: Optional configuration for log reporting levels
log(tag: string, payload?: object)
: Log an info-level messagedebug(tag: string, payload?: object)
: Log a debug-level messagewarn(tag: string, payload?: object)
: Log a warning-level messageerror(tag: string, payload?: object)
: Log an error-level messagefatal(tag: string, payload?: object)
: Log an fatal-level messagequack(tag: string, error: Error)
: Report error
The official DuckBug.io provider for sending logs to the DuckBug.io platform.
new DuckBugProvider(config: DuckConfig)
config.dsn
: Your DuckBug.io DSN (Data Source Name)
type LogProviderConfig = {
logReports: {
log?: boolean; // Enable/disable info logs (default: false)
warn?: boolean; // Enable/disable warning logs (default: true)
error?: boolean; // Enable/disable error logs (default: true)
}
}
You can create custom providers by implementing the Provider
interface:
import { Provider, LogLevel } from '@duckbug/js';
class TelegramProvider implements Provider {
constructor(private botToken: string, private chatId: string) {}
log(...args: unknown[]): void {
this.sendToTelegram('π', args);
}
warn(...args: unknown[]): void {
this.sendToTelegram('β οΈ', args);
}
error(...args: unknown[]): void {
this.sendToTelegram('π¨', args);
}
report(tag: string, level: LogLevel, payload?: object): void {
const emojiMap: Record<LogLevel, string> = {
INFO: 'π',
DEBUG: 'π¦',
WARN: 'β οΈ',
ERROR: 'π¨',
FATAL: 'π',
};
this.sendToTelegram(emojiMap[level], [tag, payload]);
}
quack(tag: string, error: Error): void {
this.sendToTelegram('π', [tag, error.message]);
}
private sendToTelegram(emoji: string, args: unknown[]) {
const message = `${emoji} ${args.join(' ')}`;
// Implementation to send message to Telegram
fetch(`https://api.telegram.org/bot${this.botToken}/sendMessage`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
chat_id: this.chatId,
text: message
})
});
}
}
// Usage
const providers = [
new DuckBugProvider({ dsn: 'your-dsn' }),
new TelegramProvider('your-bot-token', 'your-chat-id')
];
const duck = new DuckSDK(providers);
Install dependencies:
yarn install
Build the library:
yarn build
Run linting:
yarn lint
This package includes TypeScript definitions. All exports are fully typed:
import type { Provider, DuckConfig, LogLevel } from '@duckbug/js';
This SDK works in all modern browsers that support:
- ES2015+ (ES6)
- Fetch API
- JSON API
For older browsers, you may need to include polyfills.
MIT Β© DuckBug.io
- π Issues: GitHub Issues
Made with π¦ by the DuckBug.io team