Skip to content

Commit

Permalink
[FAB-15027] Validation fix
Browse files Browse the repository at this point in the history
Object that is created for internal validation not used correctly

Change-Id: I77ee4e5f7eb5a73506e114c3fa7f7a78563742f8
Signed-off-by: Matthew B. White <[email protected]>

[FAB-14093] [FAB-14097] schema update

Updated schema to have properties as map and a single return type

Change-Id: Ibbad75d106f8e54547d77d4eebc05a8dc8be08d6
Signed-off-by: awjh-ibm <[email protected]>

Change-Id: I77ee4e5f7eb5a73506e114c3fa7f7a78563742f8
Signed-off-by: Matthew B. White <[email protected]>

Updates to logger.js to satisfy updated eslint
- Required for cherry picking this commit to master
- Applied missing changes from 94df609
- Removed `to.be.ok` calls, as it's not recommended in chai: https://www.chaijs.com/api/bdd/

Signed-off-by: heatherlp <[email protected]>
Change-Id: Ib457b9c953ffa35f34b5f6c2a882c0d656e25698
  • Loading branch information
mbwhite authored and heatherlp committed Sep 17, 2019
1 parent e48d70c commit 168c7de
Show file tree
Hide file tree
Showing 17 changed files with 696 additions and 158 deletions.
128 changes: 105 additions & 23 deletions fabric-contract-api/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,68 @@

const winston = require('winston');
const loggers = {};
const SPLAT = Symbol.for('splat');
const util = require('util');
const safeStringify = require('fast-safe-stringify');
// looks odd, but this is the most efficient way of padding strings in js
const padding = ' ';

function createLogger(level, name) {
// a singleton and default logger
const {
config
} = winston;
const logger = new winston.Logger({
level,
transports: [
new winston.transports.Console({
timestamp: () => new Date().toISOString(),
handleExceptions: true,
formatter: (options) => {
return `${options.timestamp()} ${
config.colorize(options.level, options.level.toUpperCase())} ${
name ? config.colorize(options.level, `[${name}]`) : ''
} ${options.message ? options.message : ''} ${
options.meta && Object.keys(options.meta).length ? `\n\t${JSON.stringify(options.meta)}` : ''}`;
// define the formatter for Winston
// this is aimed at being a singleton
const formatter = name => winston.format.combine(
winston.format.timestamp(),
winston.format.metadata({fillExcept: ['message', 'level', 'timestamp', 'label']}),
winston.format.colorize(),
winston.format.padLevels(),
winston.format.printf((info) => {
const {timestamp, level, message} = info;
const str = (`[c-api:${name}]` + padding).substring(0, padding.length);
let out = '';
if (info[SPLAT]) {
out = info[SPLAT].map(e => {
if (e && e.error) {
if (e.error.stack) {
return e.error.stack;
} else {
return e.error.message;
}
} else {
return safeStringify(e);
}
})
});
}
return `${timestamp} ${level} ${str} ${message} ${out} `;
}
)
);

// a console based transport, again a singleton
let transport;
const getTransport = () => {
if (!transport) {
transport = new winston.transports.Console({
handleExceptions: false,
});
}
return transport;
};

// create a logger
// there is no hierachy or split of loggers; one for future versions
function createLogger (loglevel, name) {
const logger = new winston.createLogger({
level: loglevel,
format: formatter(name),
transports: [
getTransport()
],
exitOnError: false
});
return logger;
}

module.exports.getLogger = function (name = '') {
// set the logging level based on the environment variable
// configured by the peer
const level = process.env.CORE_CHAINCODE_LOGGING_LEVEL;
// map the Hyperledger Fabric standard strings to the matching Winston ones
const levelMapping = (level) => {
let loglevel = 'info';
if (typeof level === 'string') {
switch (level.toUpperCase()) {
Expand All @@ -51,10 +83,21 @@ module.exports.getLogger = function (name = '') {
break;
case 'DEBUG':
loglevel = 'debug';
break;
case 'INFO':
loglevel = 'info';
}
}
return loglevel;
};

// Exported function to get the logger for a given name
module.exports.getLogger = function (name = '') {
// set the logging level based on the environment variable
// configured by the peer
const loglevel = levelMapping(process.env.CORE_CHAINCODE_LOGGING_LEVEL);
let logger;

if (loggers[name]) {
logger = loggers[name];
logger.level = loglevel;
Expand All @@ -64,4 +107,43 @@ module.exports.getLogger = function (name = '') {
}

return logger;
};
};

// Specifically set the logging level
module.exports.setLevel = (level) => {
// set the level of all the loggers currently active
const loglevel = levelMapping(level);
process.env.CORE_CHAINCODE_LOGGING_LEVEL = loglevel;

Object.keys(loggers).forEach((name) => {
loggers[name].level = loglevel;
});
};


// This function is intended for once only use; it will setup a logger
// that will response to the unhanldedExceptions and the unhandledRejections
// Having too many transports that have handleExceptions = true results in
// node warnings about memory leaks.
function firstTime () {
if (!loggers._) {
const loglevel = levelMapping(process.env.CORE_CHAINCODE_LOGGING_LEVEL);
loggers._ = new winston.createLogger({
level: loglevel,
format: formatter('_'),
transports: [
new winston.transports.Console({
handleExceptions: true,
})
],
exitOnError: false
});


process.on('unhandledRejection', (reason, p) => {
loggers._.error('Unhandled Rejection reason ' + reason + ' promise ' + util.inspect(p));
});

}
}
firstTime();
Loading

0 comments on commit 168c7de

Please sign in to comment.