log
A package for creating logs in S3
Installation
npm install @janiscommerce/log
Configuration
ENV variables
JANIS_SERVICE_NAME
(required): The name of the service that will create the log.
API
add(bucketName, log)
Parameters: bucketName [String]
, log [Object]
Puts the recieved log into the specified S3 bucket.
Log structure
The log [Object]
parameter have the following structure:
type [String|Number]
(required): The log typeentity [String]
(required): The name of the entity that is creating the logentity_id [String]
(optional): The ID of the entity that is creating the logmessage [String]
(optional): A general message about the loglog [Object]
(optional): This property is a JSON that includes all the technical data about your log.date_created [unix_timestamp]
(optional): The date created of the log. Default will be auto-generated.id [String]
(optional): The ID of the log in UUID V4 format. Default will be auto-generated.
Log example
{
type: 'new',
entity: 'api',
entity_id: 'log',
service: 'trace',
message: '[GET] Request from 12.345.67.89 to /log',
date_created: 1559103066,
log: {
verb: 'GET',
headers: {
'x-forwarded-for': '12.345.67.89',
'x-forwarded-proto: 'https',
'x-forwarded-port: '443',
},
responseHttpCode: 200
},
id: '0acefd5e-cb90-4531-b27a-e4d236f07539'
}
on(event, callback)
Parameters: event [String]
, callback [Function]
Calls a callback when the specified event is emitted.
Errors
The errors are informed with a LogError
.
This object has a code that can be useful for a correct error handling.
The codes are the following:
Code | Description |
---|
1 | Invalid log |
2 | Invalid bucket |
3 | S3 Error |
4 | Unknown service name |
In case of error while creating your log into S3, this package will emit an event called create-error
, you can handle it using the on()
method.
Usage
const Log = require('@janiscommerce/log');
Log.add('my-bucket', {
type: 1,
entity: 'api',
entity_id: 'product',
message: '[GET] Request from 0.0.0.0 of custom_data'
});
Log.on('create-error', (log, err) => {
console.error(`An error occurred while creating the log ${err.message}`);
});
Notes
In order to connect into S3, this package requires the aws volume in the docker-compose.yml
.
volumes:
~/.aws:/root/.aws