Skip to content

Akv2021/FileLogger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FileLogger

A powerful Node.js utility for flexible file logging with support for JSON formatting, append operations, and error handling.

πŸš€ Features

Core Features

  • πŸ“ 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

Advanced Features

  • πŸ“Ž JSON pretty printing
  • ⚑ Automatic file creation
  • πŸ” Stack trace logging
  • πŸ“… Moment.js timestamp formatting
  • πŸ”’ Safe stringification
  • πŸ“¨ Email notifications (optional)

πŸ”₯ Quick Start

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
});

πŸ’‘ Use Cases

1. API Response Logging

app.get('/api/data', (req, res) => {
    fileLog({
        name: 'api_responses',
        value: res.data,
        uniqueName: true,
        moment: true
    });
});

2. Debug Logging

fileLog({
    name: 'debug',
    value: debugData,
    debug: true,
    showerr: true
});

3. Continuous Logging

// 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
});

βš™οΈ Configuration Options

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

🎯 Special Modes

JSON Mode

fileLog({
    name: 'data',
    value: { key: 'value' },
    ext: 'json',
    stringify: true
});

Debug Mode

fileLog({
    name: 'debug_log',
    value: complexObject,
    debug: true,
    showerr: true
});

Append Mode

fileLog({
    name: 'append_log',
    value: data,
    append: true,
    first: true  // for first entry
    // or
    last: true   // for last entry
});

πŸ” Help System

Get help on specific features:

fileLog('help');  // All topics
fileLog('name');  // Help on filename
fileLog('append,stringify');  // Multiple topics

🚨 Error Handling

The 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
});

πŸ› οΈ Advanced Usage

Custom Path

fileLog({
    name: 'custom_log',
    value: data,
    path: '/custom/path/to/logs/'
});

Clean Large Objects

fileLog({
    name: 'clean_log',
    value: largeObject,
    clean: true,
    debug: true
});

Unique File Names

fileLog({
    name: 'unique_log',
    value: data,
    uniqueName: true,
    moment: true  // Use readable timestamp
});

πŸ“ Best Practices

  1. Use Debug Mode for development

    fileLog({ value: data, debug: true });
  2. Handle Large Objects

    fileLog({ value: bigObject, clean: true });
  3. Structured Logging

    fileLog({
        name: 'structured_log',
        value: data,
        stringify: true,
        ext: 'json'
    });

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request

πŸ“„ License

MIT License - feel free to use and modify for your projects.

πŸ™‹β€β™‚οΈ Support

For issues and feature requests, please create an issue

About

JS Utility to create logs by printing values in JSON format in external files.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •