Skip to content

Commit

Permalink
Be 720 update log4js (#72)
Browse files Browse the repository at this point in the history
* BE-720 Refine logging by using latest log4js

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>

* BE-720 Add environment variables to control log

- LOG_LEVEL_APP=debug
- LOG_LEVEL_DB=debug
- LOG_LEVEL_CONSOLE=info
- LOG_CONSOLE_STDOUT=true (console log is forwarded to stdout, instead of file)

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>

* BE-720 Add configuration for log file to be rolled by size

Each log is rolled by both date (7days) and size (8MB)
Also updated README.md for logging configuration

Signed-off-by: Atsushi Neki <atsushin@fast.au.fujitsu.com>
  • Loading branch information
nekia authored and nfrunza committed Dec 26, 2019
1 parent 4819c9b commit 6b2393d
Show file tree
Hide file tree
Showing 23 changed files with 730 additions and 387 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Hyperledger Explorer is a simple, powerful, easy-to-use, well maintained, open s
- [10.0 Build Hyperledger Explorer ](#Build-Hyperledger-Explorer)
- [10.1 Optional: Configure Hyperledger Explorer for HTTPS ](#Configure-Hyperledger-Explorer-for-HTTPS)
- [11.0 Run Hyperledger Explorer ](#Run-Hyperledger-Explorer)
- [11.1 Configuration of logging ](#Run-Hyperledger-Explorer-Logging)
- [12.0 Optional: Run Hyperledger Explorer Using Docker ](#Run-Hyperledger-Explorer-using-Docker)
- [12.1 Docker Repository ](#Docker-Repository)
- [12.2 Run Hyperledger Explorer Using Docker Compose ](#Run-Hyperledger-Explorer-using-Docker-Compose)
Expand Down Expand Up @@ -401,7 +402,24 @@ From new terminal (if Sync Process in Standalone).
- If the Hyperledger Explorer was used previously in your browser be sure to clear the cache before relaunching.
- If Hyperledger Fabric network is deployed on other machine, please toggle DISCOVERY_AS_LOCALHOST in start.sh / syncstart.sh to 'false'.

<a name="Run-Hyperledger-Explorer-Logging" />

# 11.1 Configuration of logging <!-- do not remove this comment, ensure there is a blank line before each heading -->

By using the following environmet variables, you can control log level of each component (app, db and console). You can set these `ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < MARK < OFF` string to each level. Each file is rolled by both date (7days) and size (8MB).

* LOG_LEVEL_APP
* Log level regarding application layer. The logs are written to `logs/app/app.log`.
* default `DEBUG`
* LOG_LEVEL_DB
* Log level regarding backend layer. The logs are written to `logs/db/db.log`.
* default `DEBUG`
* LOG_LEVEL_CONSOLE
* Log level regarding console. The logs are written to `logs/console/console.log`.
* default `INFO`
* LOG_CONSOLE_STDOUT
* You can switch the destination of console log from file to standard output.
* default `false`

<a name="Run-Hyperledger-Explorer-using-Docker" />

Expand Down
74 changes: 49 additions & 25 deletions app/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

const log4js = require('log4js/lib/log4js');

const appList = [];

const path = require('path');
const fs = require('fs-extra');
const yn = require('yn');

exports.getLogger = getLogger;
exports.readAllFiles = readAllFiles;
Expand Down Expand Up @@ -64,46 +63,71 @@ function readAllFiles(dir) {
* @returns
*/
function getLogger(moduleName) {
let logger;

if (moduleName === 'PgService') {
logger = log4js.getLogger('PgService');
} else {
appList.push(moduleName);
logger = log4js.getLogger(moduleName);
}
const logger = log4js.getLogger(moduleName);

let appLog = 'logs/app/app.log';
let dbLog = 'logs/db/db.log';
let consoleLog = 'logs/console/console.log';

if (process.env.SYNC_LOG_PATH) {
appLog = `${process.env.SYNC_LOG_PATH}/app/app.log`;
dbLog = `${process.env.SYNC_LOG_PATH}/db/db.log`;
consoleLog = `${process.env.SYNC_LOG_PATH}/console/console.log`;
}

fs.ensureFileSync(appLog);
fs.ensureFileSync(dbLog);
let appLevel = 'debug';
let dbLevel = 'debug';
let consoleLevel = 'info';

if (process.env.LOG_LEVEL_APP) {
appLevel = process.env.LOG_LEVEL_APP;
}
if (process.env.LOG_LEVEL_DB) {
dbLevel = process.env.LOG_LEVEL_DB;
}
if (process.env.LOG_LEVEL_CONSOLE) {
consoleLevel = process.env.LOG_LEVEL_CONSOLE;
}

log4js.configure({
appenders: [
{
const logConfig = {
appenders: {
app: {
type: 'dateFile',
filename: appLog,
// eslint-disable-next-line spellcheck/spell-checker
pattern: '-yyyy-MM-dd',
category: appList
maxLogSize: 8 * 1024 * 1024,
daysToKeep: 7
},
{
db: {
type: 'dateFile',
filename: dbLog,
// eslint-disable-next-line spellcheck/spell-checker
pattern: '-yyyy-MM-dd',
category: ['PgService']
maxLogSize: 8 * 1024 * 1024,
daysToKeep: 7
},
console: {
type: 'dateFile',
filename: consoleLog,
maxLogSize: 8 * 1024 * 1024,
daysToKeep: 7
},
consoleFilter: {
type: 'logLevelFilter',
appender: 'console',
level: consoleLevel
}
]
});
},
categories: {
default: { appenders: ['consoleFilter', 'app'], level: appLevel },
PgService: { appenders: ['consoleFilter', 'db'], level: dbLevel }
}
};

if (process.env.LOG_CONSOLE_STDOUT) {
if (yn(process.env.LOG_CONSOLE_STDOUT)) {
logConfig.appenders.console = { type: 'console' };
}
}

logger.setLevel('DEBUG');
log4js.configure(logConfig);

return logger;
}
Expand Down
4 changes: 2 additions & 2 deletions app/persistence/fabric/CRUDService.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CRUDService {
}

logger.debug('getTxList.byOrgs ', byOrgs);
console.debug('getTxList.byOrgs ', byOrgs);
logger.debug('getTxList.byOrgs ', byOrgs);

const sqlTxListByOrgs = ` select t.creator_msp_id,t.txhash,t.type,t.chaincodename,t.createdt,channel.name as channelName from transactions as t
inner join channel on t.channel_genesis_hash=channel.channel_genesis_hash where t.blockid >= ${blockNum} and t.id >= ${txid} and t.creator_msp_id in (${orgs}) and
Expand Down Expand Up @@ -118,7 +118,7 @@ class CRUDService {
}

logger.debug('getBlockAndTxList.byOrgs ', byOrgs);
console.debug('getBlockAndTxList.byOrgs ', byOrgs);
logger.debug('getBlockAndTxList.byOrgs ', byOrgs);

const sqlBlockTxList = `select a.* from (
select (select c.name from channel c where c.channel_genesis_hash =
Expand Down
2 changes: 1 addition & 1 deletion app/persistence/fabric/MetricService.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class MetricService {
as channel_genesis_hash,c.path as path ,txcount as c from chaincodes as c inner join channel on c.channel_genesis_hash=channel.channel_genesis_hash where c.channel_genesis_hash='${channel_genesis_hash}' `);
if (c) {
c.forEach((item, index) => {
console.debug(' item ------------> ', item);
logger.debug(' item ------------> ', item);
txArray.push({
chaincodename: item.chaincodename,
channelName: item.channelname,
Expand Down
14 changes: 3 additions & 11 deletions app/persistence/postgreSQL/PgService.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ class PgService {
logger.info(`connecting to Postgresql ${connectionString}`);

this.client = new Client(this.pgconfig);

logger.info(
'Please set logger.setLevel to DEBUG in ./app/helper.js to log the debugging.'
);
}

/**
Expand All @@ -93,7 +89,7 @@ class PgService {
async handleDisconnect() {
try {
this.client.on('error', err => {
console.log('db error', err);
logger.error('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
this.handleDisconnect();
} else {
Expand All @@ -108,7 +104,7 @@ class PgService {
* To avoid a hot loop, and to allow our node script to
* Process asynchronous requests in the meantime.
*/
console.log('error when connecting to db:', err);
logger.error('error when connecting to db:', err);
setTimeout(this.handleDisconnect, 2000);
}
}
Expand Down Expand Up @@ -163,7 +159,6 @@ class PgService {
_self.client.query(addSql, addSqlParams, (err, res) => {
if (err) {
logger.error('[INSERT ERROR] - ', err.message);
console.log(err.stack);
reject(err);
return;
}
Expand All @@ -173,7 +168,7 @@ class PgService {
);
// Console.log('INSERT ID:', res.rows[0].id);
logger.debug(
'-----------------------------------------------------------------\n\n'
'-----------------------------------------------------------------'
);

resolve(res.rows[0].id);
Expand Down Expand Up @@ -218,7 +213,6 @@ class PgService {
const addSql = ` UPDATE ${tablename} set ${updateParmsStr} WHERE ${pkName} = ${pkValue} RETURNING *`;

logger.debug(`update sql is ${addSql}`);
console.log(`update sql is ${addSql}`);
_self.client.query(addSql, addSqlParams, (err, res) => {
if (err) {
logger.error('[INSERT ERROR] - ', err.message);
Expand Down Expand Up @@ -276,7 +270,6 @@ class PgService {
const addSql = ` UPDATE ${tablename} set ${updateParmsStr} WHERE ${updatewhereparm} RETURNING * `;

logger.debug(`update sql is ${addSql}`);
console.log(`update sql is ${addSql}`);
_self.client.query(addSql, addSqlParams, (err, res) => {
if (err) {
logger.error('[INSERT ERROR] - ', err.message);
Expand Down Expand Up @@ -528,7 +521,6 @@ class PgService {
return;
}

// console.log( `The solution is: ${rows.length } ` );
logger.debug(` the getRowsBySQlNoCondition ${sql}`);

if (res && res.rows) {
Expand Down
40 changes: 15 additions & 25 deletions app/platform/fabric/FabricClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class FabricClient {
this.defaultOrderer.getName()
);
} else if (persistence) {
console.log('\n ********* call to initializeDetachClient **********');
logger.info('********* call to initializeDetachClient **********');
this.initializeDetachClient(this.client_config, persistence);
}
}
Expand All @@ -186,7 +186,7 @@ class FabricClient {
*/
async initializeDetachClient(client_config, persistence) {
const name = client_config.name;
console.debug(
logger.debug(
'initializeDetachClient --> client_config ',
client_config,
' name ',
Expand All @@ -200,14 +200,14 @@ class FabricClient {
this.userName = fabricConfig.getAdminUser();
const peers = fabricConfig.getPeersConfig();

console.log('initializeDetachClient, network config) ', config);
console.log(
'\n************************************* initializeDetachClient *************************************************'
logger.info('initializeDetachClient, network config) ', config);
logger.info(
'************************************* initializeDetachClient *************************************************'
);
console.log('Error :', explorer_mess.error.ERROR_1009);
console.log('Info : ', explorer_mess.message.MESSAGE_1001);
console.log(
'************************************** initializeDetachClient ************************************************\n'
logger.info('Error :', explorer_mess.error.ERROR_1009);
logger.info('Info : ', explorer_mess.message.MESSAGE_1001);
logger.info(
'************************************** initializeDetachClient ************************************************'
);
const defaultPeerConfig = fabricConfig.getDefaultPeerConfig();
const default_peer_name = defaultPeerConfig.name;
Expand Down Expand Up @@ -250,14 +250,13 @@ class FabricClient {
}
} catch (e) {
logger.error(e);
console.error(e);
}
}

try {
newchannel.getPeer(default_peer_name);
} catch (e) {
console.error(
logger.error(
'Failed to connect to default peer: ',
default_peer_name,
' \n',
Expand Down Expand Up @@ -394,7 +393,7 @@ class FabricClient {
);
// Setting channel_genesis_hash to map
this.setChannelGenHash(channel_name, channel_genesis_hash);
console.debug(
logger.debug(
'Channel genesis hash for channel [%s] >> %s',
channel_name,
channel_genesis_hash
Expand All @@ -414,7 +413,7 @@ class FabricClient {
* @memberof FabricClient
*/
async initializeChannelFromDiscover(channel_name) {
console.debug('initializeChannelFromDiscover ', channel_name);
logger.debug('initializeChannelFromDiscover ', channel_name);
let channel = this.hfc_client.getChannel(channel_name, false);
if (!channel) {
await this.initializeNewChannel(channel_name);
Expand Down Expand Up @@ -466,25 +465,17 @@ class FabricClient {
for (const msp_id in discover_results.orderers) {
const endpoints = discover_results.orderers[msp_id].endpoints;
for (const endpoint of endpoints) {
console.log(' FabricClient.discover_results endpoint ', endpoint);
logger.info(' FabricClient.discover_results endpoint ', endpoint);
const discoveryProtocol = this.hfc_client.getConfigSetting(
'discovery-protocol'
);
const requesturl =
`${discoveryProtocol}://${endpoint.host}:` + endpoint.port;
console.log(
'\ninitializeChannelFromDiscover.discoveryProtocol ',
discoveryProtocol,
' requesturl ',
requesturl,
'\n'
);
logger.debug(
'\ninitializeChannelFromDiscover.discoveryProtocol ',
'initializeChannelFromDiscover.discoveryProtocol ',
discoveryProtocol,
' requesturl ',
requesturl,
'\n'
requesturl
);

this.newOrderer(
Expand Down Expand Up @@ -641,7 +632,6 @@ class FabricClient {
txId: this.getHFC_Client().newTransactionID(true) // Get an admin based transactionID
};
const genesisBlock = await channel.getGenesisBlock(request);
// console.log(genesisBlock)
return genesisBlock;
} catch (error) {
logger.error(
Expand Down
7 changes: 5 additions & 2 deletions app/platform/fabric/FabricConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/

const fs = require('fs');
const helper = require('../../common/helper');

const logger = helper.getLogger('FabricConfig');

/**
*
Expand Down Expand Up @@ -61,7 +64,7 @@ class FabricConfig {
* @memberof FabricConfig
*/
getTls() {
console.log('config.client.tlsEnable ', this.config.client.tlsEnable);
logger.info('config.client.tlsEnable ', this.config.client.tlsEnable);
return this.config.client.tlsEnable;
}

Expand Down Expand Up @@ -115,7 +118,7 @@ class FabricConfig {
let defChannel;
for (const x in this.config.channels) {
// Getting default channel
console.log('FabricConfig, this.config.channels ', x);
logger.info('FabricConfig, this.config.channels ', x);
if (x) {
defChannel = x;
}
Expand Down

0 comments on commit 6b2393d

Please sign in to comment.