A powerful Node.js utility for flexible file logging with support for JSON formatting, append operations, and error handling.
- π Multiple file formats support (JSON, text)
- π Append or create new files
- π― Unique file naming with timestamps
- π‘οΈ Safe handling of circular references
- π§Ή Auto-cleanup of large objects
- π Debug mode for detailed logging
- π JSON pretty printing
- β‘ Automatic file creation
- π Stack trace logging
- π Moment.js timestamp formatting
- π Safe stringification
- π¨ Email notifications (optional)
const fileLog = require('file-logger-utility');
// Basic usage
fileLog({
name: 'mylog',
value: { data: 'example' },
stringify: true
});
// Append mode
fileLog({
name: 'mylog',
value: { newData: 'example' },
append: true
});app.get('/api/data', (req, res) => {
fileLog({
name: 'api_responses',
value: res.data,
uniqueName: true,
moment: true
});
});fileLog({
name: 'debug',
value: debugData,
debug: true,
showerr: true
});// First entry
fileLog({
name: 'continuous_log',
value: firstData,
first: true
});
// Middle entries
fileLog({
name: 'continuous_log',
value: middleData,
append: true
});
// Last entry
fileLog({
name: 'continuous_log',
value: lastData,
append: true,
last: true
});| Option | Type | Default | Description |
|---|---|---|---|
| name | string | 'data' | File name for the log |
| value | any | - | Data to be logged |
| stringify | boolean | false | Auto-stringify data |
| ext | string | 'json' | File extension |
| uniqueName | boolean | false | Add timestamp to filename |
| moment | boolean | false | Use readable timestamps |
| append | boolean | false | Append to existing file |
| debug | boolean | false | Show debug information |
| folder | string | 'logs' | Output folder name |
fileLog({
name: 'data',
value: { key: 'value' },
ext: 'json',
stringify: true
});fileLog({
name: 'debug_log',
value: complexObject,
debug: true,
showerr: true
});fileLog({
name: 'append_log',
value: data,
append: true,
first: true // for first entry
// or
last: true // for last entry
});Get help on specific features:
fileLog('help'); // All topics
fileLog('name'); // Help on filename
fileLog('append,stringify'); // Multiple topicsThe utility handles various error scenarios:
- Circular references in objects
- File system errors
- Invalid JSON
- Large objects cleanup
fileLog({
name: 'error_log',
value: circularObject,
showerr: true,
force: false // Use safe stringify
});fileLog({
name: 'custom_log',
value: data,
path: '/custom/path/to/logs/'
});fileLog({
name: 'clean_log',
value: largeObject,
clean: true,
debug: true
});fileLog({
name: 'unique_log',
value: data,
uniqueName: true,
moment: true // Use readable timestamp
});-
Use Debug Mode for development
fileLog({ value: data, debug: true });
-
Handle Large Objects
fileLog({ value: bigObject, clean: true });
-
Structured Logging
fileLog({ name: 'structured_log', value: data, stringify: true, ext: 'json' });
Contributions are welcome! Please feel free to submit a Pull Request
MIT License - feel free to use and modify for your projects.
For issues and feature requests, please create an issue