---
title: "@push.rocks/smartlog-destination-file"
---
# @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:

```bash
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:

```typescript
// 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`:
   
   ```bash
   npm install @push.rocks/smartlog @push.rocks/smartlog-destination-file --save
   ```

2. Set up `SmartlogDestinationFile` as a destination in `Smartlog`:

   ```typescript
   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.

## License and Legal Information

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](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.
