<commit_before># dalee-logger

[![Build Status](https://travis-ci.org/Dalee/node-logger.svg?branch=master)](https://travis-ci.org/Dalee/node-logger)

Currently work-in-progress.

Highlights:

* Console writer
* Syslog udp4 writer (Syslog/Logstash compatible)
* `uncaughtException` and `unhandledRejection` handling (process will exit)
* `hapi.js` plugin
* `express.js` plugin (WIP)

## Usage

`npm i dalee-logger --save`

### Standalone

```
import Logger, {Syslog, Console} from 'dalee-logger';

Logger.addAdapter(Console, {});
Logger.addAdapter(Syslog, {
    'host': 'example.com',
    'port': 514
});

Logger.debug('Will write to console and send UDP syslog packet');
```

### hapi.js

Version supported: `^15.2.0`

Just register plugin in `manifest.js` as:
```
plugin: {
  register: 'dalee-logger',
  options: {
    app: 'node-daemon',
    hostname: 'example.com',
    console: {
      enabled: true
    },
    syslog: {
      host: 'example.com',
      port: 514
    }
}
```

### express.js

work-in-progress


## Logstash configuration

Sample filter for Logstash:

 * logstash will listen on port `5000` for udp packets
 * successfully parsed message will go into index named `logstash-{syslog_program}`
 * every unparsed line will go to index named `logstash-error`
 * logstash will write to Elastic on `localhost:9200`

```
input {
    udp {
        port => 5000
        type => syslog
    }
}

filter {
    grok {
      match => { "message" => "<%{POSINT:syslog_pri}>%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
      add_field => [ "received_at", "%{@timestamp}" ]
      add_field => [ "index_type", "%{syslog_program}" ]
    }

    date {
      match => [ "syslog_timestamp", "MMM d HH:mm:ss.SSS" ]
      locale => "en"
      timezone => "Europe/Moscow"
      target => "@timestamp"
    }

    syslog_pri {
    }

    # every unparsed line will go to logstash-error index
    if "_grokparsefailure" in [tags] {
        mutate {
            add_field => [ "index_type", "error" ]
        }
    }
}

output {
    elasticsearch {
        # flush_size = 1 set only for debugging purposes, should be > 1 on production
        flush_size => 1
        hosts => ["localhost:9200"]
        index => "logstash-%{index_type}"
    }
    stdout {
        # this should be disabled in production environment
        codec => rubydebug
    }
}
```
<commit_msg>badge and readme update<commit_after># dalee-logger

[![Build Status](https://travis-ci.org/Dalee/node-logger.svg?branch=master)](https://travis-ci.org/Dalee/node-logger)
[![codecov](https://codecov.io/gh/Dalee/node-logger/branch/master/graph/badge.svg)](https://codecov.io/gh/Dalee/node-logger)


Currently work-in-progress.

Highlights:

* Console writer
* Syslog udp4 writer (Syslog/Logstash compatible)
* `uncaughtException` and `unhandledRejection` handling (process will exit)
* `hapi.js` plugin
* `express.js` plugin (WIP)

## Usage

`npm i dalee-logger --save`

### Standalone

```
import Logger, {Syslog, Console} from 'dalee-logger';

Logger.addAdapter(Console, {});
Logger.addAdapter(Syslog, {
    'host': 'example.com',
    'port': 514
});

Logger.debug('Will write to console and send UDP syslog packet');
```

### hapi.js

Version supported: `^15.2.0`

Just register plugin in `manifest.js` as:
```
plugin: {
  register: 'dalee-logger',
  options: {
    app: 'node-daemon',
    hostname: 'example.com',
    console: {
      enabled: true
    },
    syslog: {
      host: 'example.com',
      port: 514
    }
}
```

### express.js

work-in-progress


## Logstash configuration

Sample filter for Logstash:

 * logstash will listen on port `5000` for udp packets
 * successfully parsed message will go into index named `logstash-{syslog_program}`
 * every unparsed line will go to index named `logstash-error`
 * logstash will write to Elastic on `localhost:9200`
 * Check out [ELK-playground](https://github.com/Dalee/elk-playground) to test with

```
input {
    udp {
        port => 5000
        type => syslog
    }
}

filter {
    grok {
      match => { "message" => "<%{POSINT:syslog_pri}>%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
      add_field => [ "received_at", "%{@timestamp}" ]
      add_field => [ "index_type", "%{syslog_program}" ]
    }

    date {
      match => [ "syslog_timestamp", "MMM d HH:mm:ss.SSS" ]
      locale => "en"
      timezone => "Europe/Moscow"
      target => "@timestamp"
    }

    syslog_pri {
    }

    # every unparsed line will go to logstash-error index
    if "_grokparsefailure" in [tags] {
        mutate {
            add_field => [ "index_type", "error" ]
        }
    }
}

output {
    elasticsearch {
        # flush_size = 1 set only for debugging purposes, should be > 1 on production
        flush_size => 1
        hosts => ["localhost:9200"]
        index => "logstash-%{index_type}"
    }
    stdout {
        # this should be disabled in production environment
        codec => rubydebug
    }
}
```

{'fraction_non_alphanumeric': 0.14865915273999222, 'fraction_numerical': 0.011659541391371939, 'mean_word_length': 3.0979299363057327, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3154225', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1750, 'n_words': 410}