A module for creating a log destination that writes to a file with support for smartlog interfaces.
Go to file
2024-05-29 14:14:15 +02:00
.vscode fix(core): update 2020-06-13 10:43:02 +00:00
test fix(core): update 2020-06-15 15:16:47 +00:00
ts fix(core): update 2020-06-15 15:16:47 +00:00
.gitignore fix(core): update 2020-06-13 10:43:02 +00:00
.gitlab-ci.yml fix(core): update 2020-06-13 10:43:02 +00:00
license fix(core): update 2020-06-13 10:43:02 +00:00
npmextra.json update tsconfig 2024-04-14 17:51:13 +02:00
package-lock.json 1.0.7 2020-06-15 15:54:13 +00:00
package.json update description 2024-05-29 14:14:15 +02:00
readme.hints.md update tsconfig 2024-04-14 17:51:13 +02:00
readme.md update tsconfig 2024-04-14 17:51:13 +02:00
tsconfig.json update npmextra.json: githost 2024-04-01 21:36:06 +02:00

@push.rocks/smartlog-destination-file

a smartlog destination for transparently writing to a file

Install

To install @push.rocks/smartlog-destination-file, use the following command with npm:

npm install @push.rocks/smartlog-destination-file --save

This will add the package to your project's dependencies.

Usage

To use @push.rocks/smartlog-destination-file in your project, you need to import and instantiate it with the path to the log file you wish to write to. This path should be absolute to ensure correct file writing regardless of the current working directory of your application.

First, ensure that your TypeScript configuration supports ECMAScript modules (ESM) syntax. Then, follow the steps below for setup and usage examples.

Basic Setup

Here is how to instantiate SmartlogDestinationFile and use it to log messages:

// Import dependencies
import { SmartlogDestinationFile } from '@push.rocks/smartlog-destination-file';
import path from 'path';

// Define the absolute path to the log file
const logFilePath = path.resolve(__dirname, 'mylogs.log');

// Create an instance of SmartlogDestinationFile
const myLogDestination = new SmartlogDestinationFile(logFilePath);

// Example log message
const logMessage = {
  context: 'ExampleApp',
  correlation: {
    id: 'unique-correlation-id',
    type: 'example'
  },
  level: 'info',
  message: 'This is an example log message',
  timestamp: Date.now(),
  type: 'log',
  data: null
};

// Log the message
myLogDestination.handleLog(logMessage).then(() => {
  console.log('Log message was successfully written to file');
}).catch((error) => {
  console.error('Error writing log message to file:', error);
});

The handleLog method is asynchronous and returns a Promise. It's important to handle both successful and unsuccessful attempts to log messages.

Advanced Usage

@push.rocks/smartlog-destination-file integrates smoothly with @pushrocks/smartlog to provide a structured logging solution. You can easily set it as a destination for your smartlog instance. Here's how to do it:

  1. Install @pushrocks/smartlog along with @push.rocks/smartlog-destination-file:

    npm install @push.rocks/smartlog @push.rocks/smartlog-destination-file --save
    
  2. Set up SmartlogDestinationFile as a destination in Smartlog:

    import { Smartlog } from '@pushrocks/smartlog';
    import { SmartlogDestinationFile } from '@push.rocks/smartlog-destination-file';
    import path from 'path';
    
    const logFilePath = path.resolve(__dirname, 'myStructuredLogs.log');
    const fileDestination = new SmartlogDestinationFile(logFilePath);
    
    // Initialize Smartlog
    const myLogger = new Smartlog({
      minLevel: 'info',
      destinations: [
        fileDestination // Your instance of SmartlogDestinationFile
      ]
    });
    
    // Use Smartlog to log a message
    myLogger.log({
      level: 'info',
      message: 'Structured log message with Smartlog and SmartlogDestinationFile',
      type: 'business',
      context: 'exampleContext'
    });
    

This setup gives you the power of structured logging with the convenience of @pushrocks/smartlog's log management capabilities, directing all logs to a file specified in SmartlogDestinationFile.

Remember, @push.rocks/smartlog-destination-file is versatile and can be adapted to various logging strategies, playing a crucial role in the observability and debugging capabilities of your application. By leveraging its simplicity and integration with @pushrocks/smartlog, you can achieve an effective logging solution with minimal setup.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.