Logger file
This commit is contained in:
@@ -0,0 +1,57 @@
|
|||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Configures the database connection
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { Pool, Client } = require('pg');
|
||||||
|
const url = require('url');
|
||||||
|
const logger = require('./logger');
|
||||||
|
//const connectionString = 'postgresql://wrenchboard:wrenchboard@10.10.10.23:5432/wrenchboard';
|
||||||
|
const connectionString = process.env.POSTGRE_URL.replace(/'/g, '');
|
||||||
|
const params = url.parse(connectionString);
|
||||||
|
const auth = params.auth.split(':');
|
||||||
|
const config = {
|
||||||
|
user: auth[0],
|
||||||
|
password: auth[1],
|
||||||
|
host: params.hostname,
|
||||||
|
port: params.port,
|
||||||
|
database: params.pathname.split('/')[1],
|
||||||
|
ssl: false
|
||||||
|
};
|
||||||
|
|
||||||
|
const postgres = new Pool(config);
|
||||||
|
|
||||||
|
postgres.on('connect', client => {
|
||||||
|
logger.info('Connected to Database');
|
||||||
|
});
|
||||||
|
|
||||||
|
postgres.on('acquire', client => {
|
||||||
|
logger.info('Client is checked out from the DB connection pool');
|
||||||
|
});
|
||||||
|
|
||||||
|
postgres.on('remove', client => {
|
||||||
|
logger.info('Client is closed & removed from the DB connection pool');
|
||||||
|
});
|
||||||
|
|
||||||
|
postgres.on('error', (err, client) => {
|
||||||
|
logger.error(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Connect to PostgreSQL
|
||||||
|
postgres.connect((err, client, release) => {
|
||||||
|
logger.info(connectionString);
|
||||||
|
if (err) {
|
||||||
|
logger.error('Error acquiring client', err.stack);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
client.query('SELECT NOW()', (err, result) => {
|
||||||
|
release();
|
||||||
|
if (err) {
|
||||||
|
logger.error('Error executing query', err.stack);
|
||||||
|
return nul;
|
||||||
|
}
|
||||||
|
logger.info(result.rows);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = postgres;
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Defines logger for the microservice
|
||||||
|
*/
|
||||||
|
|
||||||
|
var winston = require('winston');
|
||||||
|
var Papertrail = require('winston-papertrail').Papertrail;
|
||||||
|
var serviceIdentifier = require('../package.json').name;
|
||||||
|
|
||||||
|
var host = process.env.LOG_SERVICE_HOST;
|
||||||
|
var port = process.env.LOG_SERVICE_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class to provide logging facilities for the microservice
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
|
||||||
|
var Logger = function () {
|
||||||
|
|
||||||
|
// Try to route logs to third party service
|
||||||
|
if (host && port) {
|
||||||
|
// Define our transport
|
||||||
|
var transport = new Papertrail({
|
||||||
|
levels: {
|
||||||
|
debug: 0,
|
||||||
|
info: 1,
|
||||||
|
error: 3
|
||||||
|
},
|
||||||
|
colors: {
|
||||||
|
debug: 'blue',
|
||||||
|
info: 'green',
|
||||||
|
error: 'red'
|
||||||
|
},
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
json: true,
|
||||||
|
colorize: true,
|
||||||
|
logFormat: (level, message) => {
|
||||||
|
return `[${serviceIdentifier}] ${level} : ${message}`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle exceptions
|
||||||
|
transport.exceptionsLevel = 'error';
|
||||||
|
winston.handleExceptions(transport);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.info = winston.info;
|
||||||
|
this.error = winston.error;
|
||||||
|
this.debug = winston.debug;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = new Logger();
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object} Logger
|
||||||
|
* @property {function} info - Log in level `info`
|
||||||
|
* @property {function} error - Log in level `error`
|
||||||
|
* @property {function} debug - Log in level `debug`
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user