# flexible-logging-service

> General purpose remote logging module

Latest version **1.0.0** (published 2020-07-27) · GPL-3.0 license · 0 weekly downloads

## Install

```sh
npm install flexible-logging-service
pnpm add flexible-logging-service
yarn add flexible-logging-service
bun add flexible-logging-service
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-07-27 |
| First published | 2020-07-27 |
| Weekly downloads | 0 |
| License | GPL-3.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 106.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Paolo Infante |
| Maintainers | paoloinfante |
| Keywords | Flexible, Logger, Logging, Service, Log, ElasticSearch |

## Links

- npm: https://www.npmjs.com/package/flexible-logging-service
- Repository: https://github.com/paolo-projects/flexible-logger
- Homepage: https://github.com/paolo-projects/flexible-logger#readme
- Issues: https://github.com/paolo-projects/flexible-logger/issues
- npm.io page: https://npm.io/package/flexible-logging-service

## Dependencies (2)

- [@maxmind/geoip2-node](https://npm.io/package/@maxmind/geoip2-node.md) ^1.4.0
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) ^7.8.0

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2020-07-27

## README

![Node.js CI](https://github.com/paolo-projects/flexible-logger/workflows/Node.js%20CI/badge.svg)
# Flexible Logger

A simple logging package for nodejs. Oriented towards flexibility, supports logging to an ElasticSearch instance by default through the `ElasticSearchDriver` class.

Support for GeoLite IP lookup is immediate, you just need to extend the `IpLogEntry` class.

Support for access logging of an express web server is out-of-the-box through the `AccessLogEntry` class, which will extract all the useful information from the Express `Request` object.

Support for additional logging facilities is easily accomplished by extending the interface `LoggerDriver` which has just one method: `write(LogEntry)`.

Additionally, to limit the rate of network requests, the indexing can be bulked by moving it to an additional node.js instance using the UDP driver.
Set up the `LogUdpServer` on a different instance running inside your network and use the `UdpDriver` to send the logs to it.

Usage:
```typescript
import FlexibleLogger from 'flexible-logger';
...

/* Instanciated asynchronously */

const Logger = await FlexibleLogger.with(new ElasticSearchDriver({
    setup: { ... }, // ElasticSearch Configuration
    indexPattern: 'my-index' // Index where documents will be inserted
    }), 
    'path/to/geolite2-city.mmdb' // Path to the GeoLite2 City database
);

/* or */

FlexibleLogger.with(...).then(Logger => {
    ...
});

/* Instanciated synchronously
 * Might be a better choice especially if you need a global instance that
 * gets initialized once and gets called in your code like a Singleton
 */

const Logger = FlexibleLogger.withSync( ... );
```
Logging...
```typescript
Logger.log(new AccessLogEntry(
    req, // the Express Request object
    'access-log' // context info
));

/* You can use one of the builtin LogEntries or 
 * you can subclass one of them to add custom data 
 */

class MyLogEntry extends IpLogEntry {
    anotherProperty: any;
    
    constructor(context: string, ip: string, myCustomProperty: number) {
        // if you extend IpLogEntry class and you specified a GeoLite db, 
        // the ip will be parsed into geo location data
        super(context, ip, "MyLogEntry");
        
        // the body property is the one that will be sent to the logging service
        this.body.my_property = myCustomProperty;
        
        // or you can add new a new one and write a new driver to handle it
        this.anotherProperty = "Cheese Cake";
    }
}

```
A simple access-log middleware for Express servers
```typescript
app.use((req, res, next) => {
    Logger.log(new AccessLogEntry(req, "access-log"));
    next();
});
```
Usage with the UDP Driver
```typescript
const Logger = FlexibleLogger.withSync(
    new UdpDriver('localhost', 12345)
);
```
which requires a server listening on the given UDP port
```typescript
const Server = new LogUdpServer(
    new ElasticSearchDriver({ ... }), 
    {
        port: 1234,
        address: 'localhost'
    }
);

Server.bind();
Server.poll();
```
The UDP server can be configured with the following options
```typescript
export interface ServerConfig {
    /**
     * Port to bind the server on
     */
    port: number;
    /**
     * Address to bind the server on. Defaults to 0.0.0.0 (any)
     */
    address?: string;
    /**
     * If an error occurs, bind again the port after a timeout. Default is true
     */
    retryOnError?: boolean;
    /**
     * Timeout in ms to wait before retrying binding the port. Default is 10000
     */
    bindRetryTimeout?: number;
    /**
     * Rate in ms at which to flush the queue and send it to the service. Default is 20000
     */
    queuePollRate?: number;
    /**
     * Minimum amount of entries before the messages are sent to the service. Default is 1
     */
    logEntriesTreshold?: number;
    /**
     * If set to true, messages queue will always be flushed even if an error prevented them
     * from being sent to the service. Default is true
     */
    alwaysFlush?: boolean;
}
```

---
_Source: https://npm.io/package/flexible-logging-service · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
