---
title: "@push.rocks/smartlog-receiver"
---
# @push.rocks/smartlog-receiver
a receiver for smartlog-destination-receiver

## Install

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

```bash
npm install @push.rocks/smartlog-receiver --save
```

or with yarn:

```bash
yarn add @push.rocks/smartlog-receiver
```

Ensure that you have TypeScript and Node.js installed in your development environment as this module leverages TypeScript for type definitions.

## Usage

The `@push.rocks/smartlog-receiver` module is designed to receive log messages sent from applications using the `smartlog` logging system. It allows for centralized logging by receiving, validating, and processing log messages in a secure and controlled manner. Below, we'll go through how to set up and use `smartlog-receiver` in your project.

### Setting up SmartlogReceiver

First, make sure to import `SmartlogReceiver` from the package, along with the types it depends on if necessary.

```typescript
import { SmartlogReceiver, ISmartlogReceiverOptions } from '@push.rocks/smartlog-receiver';
import { Smartlog } from '@pushrocks/smartlog';
```

`SmartlogReceiver` requires an instance of `Smartlog` for logging internal messages and potentially logging the received log messages. Ensure you initialize `Smartlog` first:

```typescript
const smartlogInstance = new Smartlog({
  logContext: {
    company: 'YourCompany',
    companyunit: 'YourUnit',
    environment: 'production',
    runtime: 'node',
    version: '1.0.0'
  },
  minimumLogLevel: 'info',
});
smartlogInstance.enableConsole(); // Enabling console output for demonstration purposes
```

Now, instantiate the `SmartlogReceiver` with the necessary options. You need to provide a passphrase for authentication and a validator function that decides whether to accept or reject incoming messages based on your criteria.

```typescript
const smartlogReceiver = new SmartlogReceiver({
  smartlogInstance: smartlogInstance,
  passphrase: 'yourSecretPassphrase',
  validatorFunction: async (logPackage) => {
    // Example validation: accept only if message contains 'accepted'
    return logPackage.message.includes('accepted');
  },
});
```

### Handling Log Messages

To handle authenticated log messages, you'd typically have an endpoint in your application that receives log packages. Here, we'll simulate receiving a log package.

```typescript
import { ILogPackageAuthenticated } from '@pushrocks/smartlog-interfaces';
import * as smarthash from '@pushrocks/smarthash';

// Simulated received log message
const receivedLogPackage: ILogPackageAuthenticated = {
  auth: smarthash.sha256FromStringSync('yourSecretPassphrase'), // Generate hash of your passphrase for authentication
  logPackage: {
    timestamp: Date.now(),
    context: {
      company: 'YourCompany',
      companyunit: 'YourUnit',
      environment: 'production',
      runtime: 'node',
      version: '1.0.0'
    },
    level: 'info',
    type: 'log',
    message: 'This is an accepted log message',
    correlation: {
      id: 'unique-correlation-id',
      type: 'transaction'
    }
  }
};

// Handler function to process received log packages
async function handleReceivedLog(logPackage: ILogPackageAuthenticated) {
  const result = await smartlogReceiver.handleAuthenticatedLog(logPackage);
  console.log(result); // Should output { status: 'ok' } for successfully processed messages
}

handleReceivedLog(receivedLogPackage);
```

Ensure your validation and handling logic fits the security and architectural needs of your application.

### Advanced Usage

- **Batch Processing:** `SmartlogReceiver` also supports processing batches of logs efficiently through the method `handleManyAuthenticatedLogs`, facilitating bulk log management.

- **Customizing Smartlog:** Since `SmartlogReceiver` uses `Smartlog` for its operations, you can harness the full power of `Smartlog` for detailed customization, such as setting up different log destinations, filtering log levels, and more.

By setting up `@push.rocks/smartlog-receiver`, you're enabling a robust and flexible system to manage your application's logging infrastructure, making it easier to monitor and debug with centralized logs.

## 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.
