wl-logs-elk
v1.0.2
Published
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Downloads
4
Readme
wl-logs-elk
wl-logs-elk
is a simple and universal logging library with support for multiple transports. It provides configuration options to handle logs in the console, local file, or both.
Getting Started
This package is designed to work with a local ELK (Elasticsearch, Logstash, Kibana) setup. Follow the steps below to configure ELK on your system:
Elasticsearch: Download Elasticsearch from https://www.elastic.co/downloads/ and start Elasticsearch by executing the command
/bin/elasticsearch.bat
.Kibana: Before starting Kibana, open the
config/kibana.yml
file and uncomment the lineelasticsearch.hosts: ["http://localhost:9200"]
. Start the Kibana service by executing the command/bin/kibana.bat
.Logstash: Create a
logstash.conf
file at the root directory of your project with the following content:input{ file{ path=>"<../logs/service.log>"//your log file path start_position=>"beginning" } } output{ elasticsearch{ hosts=>[ "localhost:9200" ] }stdout{ codec=>rubydebug } }
For starting the logstash services execute command : logstash -f logstash.conf
- [ ] Follow the instructions mentioned in this Reference video for additional guidance.
Add your files
- [ ] Create or upload files
- [ ] Add files using the command line or push an existing Git repository with the following command:
cd existing_repo
git remote add origin https://gitlab.com/wavelabs/code-accelerators/apis/nodejs/log_management_elk.git
git branch -M main
git push -uf origin main
Usage
To use this package, import it and provide the necessary parameters as shown below:
const { LoggerService } = require('wl-logs-elk');
const logData = {
LogLevel: 'error', // possible values are [error/warn/info/debug/trace]
LogFilePath: './logs/service.log',
ElasticSearchHost: 'http://localhost:9200',
KibanaIndxPrefix: 'myapp-',
Service: 'local',
HostName: 'localhost',
LogStream : 'both'
};
const log = LoggerService(logData);
module.exports = { log };
Parameters:
The following parameters can be provided in the logData
object:
LogLevel
: Set the log level for the entire application.LogFilePath
: Specify the log file path. This parameter is mandatory if LogStream is set to either file or both. If LogFilePath is not provided, an error will be thrown: "Error: LogFilePath should not be empty if LogStream is set to file or both."ElasticSearchHost
: The URL where Elasticsearch services are running.KibanaIndxPrefix
: Preferred index prefix to aid in discovering indexes in Kibana.Service
: Service name.HostName
: Application host name.LogStream
: Possible values are file, console, or both.
Logging Examples
To log messages, import the log object from the service file and use it as follows:
const { log } = require('./src/app/service');
const logData = {
sessionId: '3232323',
data: { otp: 100 },
APIId: 'API002',
api: '/api/validate'
};
log.info('service api', logData);
log.error('error message');
log.warn('warn message');
log.debug('debug message');
log.trace('debug message');
Feel free to customize the log data according to your needs.
Log Levels:
- error
- warn
- info
- debug
- trace