Skip to content

Commit

Permalink
[FAB-14736] Update Logging
Browse files Browse the repository at this point in the history
Fix typescript definitions where the logger was involed
Updated to use Winston 3.0 that had updated typescript definitions
Fixed updates to apis as a result of Winston 3 updated, including simplified formatter

Change-Id: I2e99de0b0a82d41eafd6e17c0f9454f0011fee2f
Signed-off-by: Matthew B. White <[email protected]>
  • Loading branch information
mbwhite committed Mar 25, 2019
1 parent c406864 commit 94df609
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 54 deletions.
29 changes: 15 additions & 14 deletions fabric-contract-api/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@
const winston = require('winston');
const loggers = {};

function createLogger(level, name) {
function createLogger(loglevel, name) {
// a singleton and default logger
const {
config
} = winston;
const logger = new winston.Logger({
level,
const logger = new winston.createLogger({
level:loglevel,
format: winston.format.combine(
winston.format.splat(),
winston.format.colorize(),
winston.format.timestamp(),
winston.format.align(),
winston.format.simple(),
winston.format.printf((info) => {
const {timestamp, level, message} = info;
return `${timestamp} ${level} [${name}] ${message}`;
}
)
),
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)}` : ''}`;
}
})
],
exitOnError: false
Expand Down
2 changes: 1 addition & 1 deletion fabric-contract-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"get-params": "^0.1.2",
"reflect-metadata": "^0.1.12",
"winston": "^2.4.1"
"winston": "^3.2.1"
},
"types": "./types/index.d.ts",
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion fabric-contract-api/test/typescript/smartcontract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ export default class TestContractOne extends Contract {
const clientIdentity: ClientIdentity = ctx.clientIdentity;

// test that the name returns a string
const ns: string = this.getName();
const ns: string = this.getName();

// add in some logging
ctx.logging.setLevel('DEBUG');
ctx.logging.getLogger().info('Output from the test');
}
}

Expand Down
11 changes: 5 additions & 6 deletions fabric-contract-api/test/unit/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/* global describe it */
'use strict';

const winston = require('winston');
const chai = require('chai');
const expect = chai.expect;

Expand All @@ -18,7 +17,7 @@ describe('Logger', () => {
it ('should create a new logger name unknown', () => {
const log = Logger.getLogger('unknown name');

expect(log instanceof winston.Logger).to.be.ok;
expect(log).to.be.ok;
expect(log.level).to.deep.equal('info');
});

Expand All @@ -42,7 +41,7 @@ describe('Logger', () => {

const log = Logger.getLogger();

expect(log instanceof winston.Logger).to.be.ok;
expect(log).to.be.ok;
expect(log.level).to.deep.equal('fatal');
});

Expand All @@ -51,7 +50,7 @@ describe('Logger', () => {

const log = Logger.getLogger();

expect(log instanceof winston.Logger).to.be.ok;
expect(log).to.be.ok;
expect(log.level).to.deep.equal('error');
});

Expand All @@ -60,7 +59,7 @@ describe('Logger', () => {

const log = Logger.getLogger();

expect(log instanceof winston.Logger).to.be.ok;
expect(log).to.be.ok;
expect(log.level).to.deep.equal('warn');
});

Expand All @@ -69,7 +68,7 @@ describe('Logger', () => {

const log = Logger.getLogger();

expect(log instanceof winston.Logger).to.be.ok;
expect(log).to.be.ok;
expect(log.level).to.deep.equal('debug');
});
});
Expand Down
6 changes: 3 additions & 3 deletions fabric-contract-api/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
*/

declare module 'fabric-contract-api' {
import { LoggerInstance } from 'winston';
import { Logger } from 'winston';
import { ChaincodeStub, ClientIdentity } from 'fabric-shim';

export class Context {
stub: ChaincodeStub;
clientIdentity: ClientIdentity;
logger: {
logging: {
setLevel: (level: string) => void,
getLogger: (name?: string) => LoggerInstance
getLogger: (name?: string) => Logger
}
}

Expand Down
29 changes: 15 additions & 14 deletions fabric-shim/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@
const winston = require('winston');
const loggers = {};

function createLogger(level, name) {
function createLogger(loglevel, name) {
// a singleton and default logger
const {
config
} = winston;
const logger = new winston.Logger({
level,
const logger = new winston.createLogger({
level:loglevel,
format: winston.format.combine(
winston.format.splat(),
winston.format.colorize(),
winston.format.timestamp(),
winston.format.align(),
winston.format.simple(),
winston.format.printf((info) => {
const {timestamp, level, message} = info;
return `${timestamp} ${level} [${name}] ${message}`;
}
)
),
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)}` : ''}`;
}
})
],
exitOnError: false
Expand Down
3 changes: 1 addition & 2 deletions fabric-shim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@
"@ampretia/x509": "^0.4.0",
"@types/google-protobuf": "^3.2.7",
"@types/node": "^8.9.4",
"@types/winston": "^2.3.9",
"ajv": "^6.5.5",
"fs-extra": "^7.0.1",
"grpc": "1.17.0-pre1",
"jsrsasign": "^8.0.4",
"reflect-metadata": "^0.1.12",
"winston": "^2.4.1",
"winston": "^3.2.1",
"yargs": "^10.0.2",
"yargs-parser": "^11.0.0",
"protobufjs": "5.0.3"
Expand Down
6 changes: 3 additions & 3 deletions fabric-shim/test/typescript/chaincode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import { Shim,
} from 'fabric-shim';

import { Timestamp } from 'google-protobuf/google/protobuf/timestamp_pb';
import { LoggerInstance } from 'winston';
import { Logger } from 'winston';

class TestTS implements ChaincodeInterface {
async Init(stub: ChaincodeStub): Promise<ChaincodeResponse> {
const logger: LoggerInstance = shim.newLogger('init');
const logger: Logger = shim.newLogger('init');
return shim.success();
}

async Invoke(stub: ChaincodeStub): Promise<ChaincodeResponse> {
const logger: LoggerInstance = Shim.newLogger('invoke');
const logger: Logger = Shim.newLogger('invoke');
const args: string[] = stub.getArgs();
const strArgs: string[] = stub.getStringArgs();
const {fcn, params} = stub.getFunctionAndParameters();
Expand Down
11 changes: 5 additions & 6 deletions fabric-shim/test/unit/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
/* global describe it */
'use strict';

const winston = require('winston');
const chai = require('chai');
const expect = chai.expect;

Expand All @@ -19,7 +18,7 @@ describe('Logger', () => {
process.env.CORE_CHAINCODE_LOGGING_SHIM = '';
const log = Logger.getLogger('unknown name');

expect(log instanceof winston.Logger).to.be.ok;
expect(log).to.be.ok;
expect(log.level).to.deep.equal('info');
});

Expand All @@ -43,7 +42,7 @@ describe('Logger', () => {

const log = Logger.getLogger();

expect(log instanceof winston.Logger).to.be.ok;
expect(log).to.be.ok;
expect(log.level).to.deep.equal('fatal');
});

Expand All @@ -52,7 +51,7 @@ describe('Logger', () => {

const log = Logger.getLogger();

expect(log instanceof winston.Logger).to.be.ok;
expect(log).to.be.ok;
expect(log.level).to.deep.equal('error');
});

Expand All @@ -61,7 +60,7 @@ describe('Logger', () => {

const log = Logger.getLogger();

expect(log instanceof winston.Logger).to.be.ok;
expect(log).to.be.ok;
expect(log.level).to.deep.equal('warn');
});

Expand All @@ -70,7 +69,7 @@ describe('Logger', () => {

const log = Logger.getLogger();

expect(log instanceof winston.Logger).to.be.ok;
expect(log).to.be.ok;
expect(log.level).to.deep.equal('debug');
});
});
Expand Down
6 changes: 3 additions & 3 deletions fabric-shim/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare module 'fabric-shim' {

import { Timestamp } from 'google-protobuf/google/protobuf/timestamp_pb';
import { EventEmitter } from 'events';
import { LoggerInstance } from 'winston';
import { Logger } from 'winston';

interface ChaincodeResponse {
status: number;
Expand All @@ -17,13 +17,13 @@ declare module 'fabric-shim' {
}

export function error(msg: Buffer): ChaincodeResponse;
export function newLogger(name: string): LoggerInstance;
export function newLogger(name: string): Logger;
export function start(chaincode: ChaincodeInterface): any;
export function success(payload?: Buffer): ChaincodeResponse;

export class Shim {
static error(msg: Buffer): ChaincodeResponse;
static newLogger(name: string): LoggerInstance;
static newLogger(name: string): Logger;
static start(chaincode: ChaincodeInterface): any;
static success(payload?: Buffer): ChaincodeResponse;
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"fabric-contract-api": "file:./fabric-contract-api",
"fabric-shim": "file:./fabric-shim",
"fabric-shim-crypto": "file:./fabric-shim-crypto",
"fs-extra": "^7.0.1",
"jsrsasign": "^8.0.4"
},
"devDependencies": {
Expand Down

0 comments on commit 94df609

Please sign in to comment.