-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bif-common): adds new pkg with logger
It is a universal package by design meaning that it works in the browser and also in NodeJS which allows both the API server and the cockpit to use the logger from the common package. Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
- Loading branch information
Showing
17 changed files
with
225 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# `@hyperledger-labs/bif-common` | ||
|
||
> TODO: description | ||
## Usage | ||
|
||
``` | ||
// TODO: DEMONSTRATE API | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"name": "@hyperledger-labs/bif-common", | ||
"version": "0.2.0", | ||
"description": "Universal library used by both front end and back end components of BIF. Aims to be a developer swiss army knife.", | ||
"main": "dist/bif-common.node.umd.js", | ||
"mainMinified": "dist/bif-common.node.umd.min.js", | ||
"browser": "dist/bif-common.web.umd.js", | ||
"browserMinified": "dist/bif-common.web.umd.min.js", | ||
"module": "dist/lib/main/typescript/index.js", | ||
"types": "dist/types/main/typescript/index.d.ts", | ||
"files": [ | ||
"dist/*" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"engines": { | ||
"node": ">=10", | ||
"npm": ">=6" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/hyperledger-labs/blockchain-integration-framework.git" | ||
}, | ||
"keywords": [ | ||
"Hyperledger", | ||
"Blockchain", | ||
"Interoperability", | ||
"Integration" | ||
], | ||
"author": { | ||
"name": "Peter Somogyvari", | ||
"email": "peter.somogyvari@accenture.com", | ||
"url": "https://accenture.com" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Please add yourself to the list of contributors", | ||
"email": "your.name@example.com", | ||
"url": "https://example.com" | ||
} | ||
], | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/hyperledger-labs/blockchain-integration-framework/issues" | ||
}, | ||
"homepage": "https://github.com/hyperledger-labs/blockchain-integration-framework#readme", | ||
"dependencies": { | ||
"loglevel": "1.6.7", | ||
"loglevel-plugin-prefix": "0.8.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './public-api'; |
15 changes: 15 additions & 0 deletions
15
packages/bif-common/src/main/typescript/logging/logger-provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Logger, ILoggerOptions } from './logger'; | ||
|
||
export class LoggerProvider { | ||
|
||
private static loggers: Map<string, Logger> = new Map(); | ||
|
||
public static getOrCreate(loggerOptions: ILoggerOptions) { | ||
let logger: Logger | undefined = LoggerProvider.loggers.get(loggerOptions.label); | ||
if (!logger) { | ||
logger = new Logger(loggerOptions); | ||
LoggerProvider.loggers.set(loggerOptions.label, logger); | ||
} | ||
return logger; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import libLogLevel, { Logger as LogLevelLogger, levels } from 'loglevel'; | ||
import prefix from 'loglevel-plugin-prefix'; | ||
|
||
prefix.reg(libLogLevel); | ||
|
||
prefix.apply(libLogLevel, { | ||
template: '[%t] %l (%n):', | ||
levelFormatter(level) { | ||
return level.toUpperCase(); | ||
}, | ||
nameFormatter(name) { | ||
return name || 'global'; | ||
}, | ||
timestampFormatter(date) { | ||
return date.toISOString(); | ||
}, | ||
}); | ||
|
||
export interface ILoggerOptions { | ||
label: string; | ||
level?: string; | ||
} | ||
|
||
/** | ||
* Levels: | ||
* - error: 0, | ||
* - warn: 1, | ||
* - info: 2, | ||
* - debug: 3, | ||
* - trace: 4 | ||
*/ | ||
export class Logger { | ||
|
||
private readonly backend: LogLevelLogger; | ||
|
||
constructor(public readonly options: ILoggerOptions) { | ||
const level: string = options.level || 'warn'; | ||
this.backend = libLogLevel.getLogger(options.label); | ||
this.backend.setLevel(level.toUpperCase() as any); | ||
} | ||
|
||
public async shutdown(gracePeriodMillis: number = 60000): Promise<void> { | ||
this.backend.info('Shut down logger OK.'); | ||
} | ||
|
||
public error(...msg: any[]): void { | ||
this.backend.error(...msg); | ||
} | ||
|
||
public warn(...msg: any[]): void { | ||
this.backend.warn(...msg); | ||
} | ||
public info(...msg: any[]): void { | ||
this.backend.info(...msg); | ||
} | ||
public debug(...msg: any[]): void { | ||
this.backend.debug(...msg); | ||
} | ||
public trace(...msg: any[]): void { | ||
this.backend.trace(...msg); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { LoggerProvider } from './logging/logger-provider'; | ||
export { Logger, ILoggerOptions } from './logging/logger'; |
11 changes: 11 additions & 0 deletions
11
packages/bif-common/src/test/typescript/integration/api-surface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// tslint:disable-next-line: no-var-requires | ||
const tap = require('tap'); | ||
import { Logger, LoggerProvider } from '../../../main/typescript/public-api'; | ||
|
||
tap.pass('Test file can be executed'); | ||
|
||
tap.test('Library can be loaded', (assert: any) => { | ||
assert.plan(2); | ||
assert.ok(Logger); | ||
assert.ok(LoggerProvider); | ||
}); |
Empty file.
11 changes: 11 additions & 0 deletions
11
packages/bif-common/src/test/typescript/unit/api-surface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// tslint:disable-next-line: no-var-requires | ||
const tap = require('tap'); | ||
import { Logger, LoggerProvider } from '../../../main/typescript/public-api'; | ||
|
||
tap.pass('Test file can be executed'); | ||
|
||
tap.test('Library can be loaded', (assert: any) => { | ||
assert.plan(2); | ||
assert.ok(Logger); | ||
assert.ok(LoggerProvider); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./dist/lib/", /* Redirect output structure to the directory. */ | ||
"declarationDir": "dist/types", | ||
}, | ||
"include": [ | ||
"./src" | ||
] | ||
} |