fable-log
v3.0.17
Published
A simple logging wrapper.
Readme
Fable-Log
A flexible, extensible logging framework for Node.js and browser applications.
Why Fable-Log?
Although there's no shortage of logging libraries, the effort to set up consistent, configurable logging across applications is not trivial. Want MongoDB logging in production and console logging for tests? Multiple output destinations? Browser compatibility? Fable-Log provides a simple, drop-in solution with a standard configuration interface.
Features
- Multiple Log Streams - Route logs to console, files, or custom destinations simultaneously
- Six Log Levels - trace, debug, info, warn, error, fatal
- Browser Compatible - Works in both Node.js and browser environments
- Extensible Providers - Create custom log providers for any destination
- Time Tracking - Built-in methods for timing and profiling operations
- Datum Decoration - Transform log data before output
- Zero Configuration - Works out of the box with sensible defaults
Installation
npm install fable-logQuick Start
const FableLog = require('fable-log');
const log = new FableLog();
log.initialize();
// Log at any level
log.trace('Detailed trace information');
log.debug('Debug information');
log.info('Informational message');
log.warn('Warning message');
log.error('Error occurred');
log.fatal('Fatal error');
// Include data objects
log.info('User logged in', { userId: 123, ip: '192.168.1.1' });Configuration
Pass a settings object to customize behavior:
const log = new FableLog({
Product: 'MyApplication',
ProductVersion: '1.0.0',
LogStreams: [
{
loggertype: 'console',
level: 'debug',
showtimestamps: true,
formattedtimestamps: true
},
{
loggertype: 'simpleflatfile',
level: 'info',
path: './logs/application.log'
}
]
});
log.initialize();Log Levels
| Level | Description |
|-------|-------------|
| trace | Detailed debugging information |
| debug | Debug information |
| info | General informational messages |
| warn | Warning conditions |
| error | Error conditions |
| fatal | Critical/fatal errors |
Built-in Providers
Console Provider
{
loggertype: 'console',
level: 'trace',
showtimestamps: true,
formattedtimestamps: true,
Context: 'MyApp'
}Simple Flat File Provider
{
loggertype: 'simpleflatfile',
level: 'info',
path: './logs/app.log'
}Time Tracking
Built-in methods for measuring operation durations:
const startTime = log.getTimeStamp();
// ... perform operation ...
log.logTimeDeltaRelative(startTime, 'Operation complete');
// Output: "Operation complete logged at (epoch 1705315800000) took (1523ms)"
log.logTimeDeltaRelativeHuman(startTime, 'Long operation');
// Output: "Long operation ... took (125000ms) or (00:02:05.000)"Browser Usage
Fable-Log works in browser environments with automatic provider adaptation:
// With bundler (webpack, rollup, etc.)
import FableLog from 'fable-log';
const log = new FableLog();
log.initialize();
log.info('Hello from the browser!');Custom Providers
Create custom providers by extending the base logger:
const BaseLogger = require('fable-log').LogProviderBase;
class MyCustomProvider extends BaseLogger {
write(pLevel, pLogText, pObject) {
// Your implementation
}
}
// Register and use
log._Providers.mycustom = MyCustomProvider;Documentation
Full documentation is available in the docs folder:
Viewing Documentation Locally
Serve the docs folder with any static server:
npx docsify-cli serve docsThen open http://localhost:3000 in your browser.
Part of the Fable Ecosystem
Fable-Log is designed to work seamlessly with other Fable packages:
- fable - Application services framework
- fable-serviceproviderbase - Service provider base class
- pict - UI framework
Testing
Run the test suite:
npm testRun with coverage:
npm run coverageRelated Packages
- fable - Application services framework
- fable-log-logger-bunyan - Bunyan logger for Fable-Log
- fable-serviceproviderbase - Service provider base class
License
MIT
Contributing
Pull requests are welcome. For details on our code of conduct, contribution process, and testing requirements, see the Retold Contributing Guide.
