-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logger): add Logger Feature Service (#413)
- Loading branch information
1 parent
4221237
commit 97d5f3e
Showing
9 changed files
with
242 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// @ts-check | ||
|
||
module.exports = [ | ||
{ | ||
path: 'lib/cjs/index.js', | ||
limit: '1 KB' | ||
} | ||
]; |
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,36 @@ | ||
# @feature-hub/logger | ||
|
||
[![Package Version][package-badge]][package-npm] | ||
[![Website][website-badge]][website] [![API][api-badge]][api] | ||
|
||
A Feature Service for providing a common logging integration to all Feature Hub | ||
consumers. | ||
|
||
## Installation | ||
|
||
### Using Yarn | ||
|
||
```sh | ||
yarn add @feature-hub/logger | ||
``` | ||
|
||
### Using NPM | ||
|
||
```sh | ||
npm install @feature-hub/logger | ||
``` | ||
|
||
--- | ||
|
||
Copyright (c) 2019 SinnerSchrader Deutschland GmbH. Released under the terms of | ||
the [MIT License][license]. | ||
|
||
[api]: https://feature-hub.io/@feature-hub/logger/ | ||
[api-badge]: | ||
https://img.shields.io/badge/API-%40feature--hub%2Flogger-%23ea3458.svg | ||
[license]: https://github.com/sinnerschrader/feature-hub/blob/master/LICENSE | ||
[package-badge]: https://img.shields.io/npm/v/@feature-hub/logger.svg | ||
[package-npm]: https://www.npmjs.com/package/@feature-hub/logger | ||
[website]: https://feature-hub.io/ | ||
[website-badge]: | ||
https://img.shields.io/badge/Website-feature--hub.io-%23500dc5.svg |
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,29 @@ | ||
{ | ||
"name": "@feature-hub/logger", | ||
"version": "0.0.0", | ||
"description": "A Feature Service for providing a common logging integration to all Feature Hub consumers.", | ||
"homepage": "https://feature-hub.io/", | ||
"bugs": { | ||
"url": "https://github.com/sinnerschrader/feature-hub/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/sinnerschrader/feature-hub.git" | ||
}, | ||
"license": "MIT", | ||
"author": "SinnerSchrader Deutschland GmbH", | ||
"files": [ | ||
"lib", | ||
"!__tests__" | ||
], | ||
"sideEffects": false, | ||
"main": "lib/cjs/index.js", | ||
"module": "lib/esm/index.js", | ||
"typings": "lib/cjs/index.d.ts", | ||
"dependencies": { | ||
"@feature-hub/core": "^1.1.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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,117 @@ | ||
// tslint:disable:no-implicit-dependencies | ||
|
||
import { | ||
FeatureServiceEnvironment, | ||
FeatureServiceProviderDefinition | ||
} from '@feature-hub/core'; | ||
import {Stub, Stubbed, stubMethods} from 'jest-stub-methods'; | ||
import {Logger, SharedLogger, defineLogger} from '..'; | ||
|
||
describe('defineLogger', () => { | ||
let mockEnv: FeatureServiceEnvironment<undefined, {}>; | ||
|
||
let loggerDefinition: FeatureServiceProviderDefinition<SharedLogger>; | ||
|
||
beforeEach(() => { | ||
mockEnv = {config: undefined, featureServices: {}}; | ||
loggerDefinition = defineLogger(); | ||
}); | ||
|
||
it('creates a Logger definition', () => { | ||
expect(loggerDefinition.id).toBe('s2:logger'); | ||
expect(loggerDefinition.dependencies).toBeUndefined(); | ||
}); | ||
|
||
describe('#create', () => { | ||
it('creates a shared Feature Service containing version 1.0.0', () => { | ||
const sharedLogger = loggerDefinition.create(mockEnv); | ||
|
||
expect(sharedLogger['1.0.0']).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('Logger', () => { | ||
let logger: Logger; | ||
|
||
describe('with the default createConsumerLogger function', () => { | ||
let stubbedConsole: Stubbed<Console>; | ||
|
||
beforeEach(() => { | ||
stubbedConsole = stubMethods(console); | ||
|
||
logger = loggerDefinition.create(mockEnv)['1.0.0']('test:id') | ||
.featureService; | ||
}); | ||
|
||
afterEach(() => { | ||
stubbedConsole.restore(); | ||
}); | ||
|
||
const loggerMethods: (keyof Logger)[] = [ | ||
'trace', | ||
'debug', | ||
'info', | ||
'warn', | ||
'error' | ||
]; | ||
|
||
for (const method of loggerMethods) { | ||
describe(`#${method}`, () => { | ||
it(`delegates to console.${method}`, () => { | ||
logger[method]('test'); | ||
|
||
expect(stubbedConsole.stub[method].mock.calls).toEqual([['test']]); | ||
}); | ||
|
||
it('preserves the call stack', () => { | ||
stubbedConsole.stub[method].mockImplementationOnce(() => { | ||
throw new Error(`mock error for ${method}}`); | ||
}); | ||
|
||
expect.assertions(2); | ||
|
||
try { | ||
logger[method]('test'); | ||
} catch (error) { | ||
expect(error.stack).toMatch( | ||
/logger\/src\/__tests__\/index.test\.(js|ts)/ | ||
); | ||
|
||
expect(error.stack).not.toMatch(/logger\/src\/index\.(js|ts)/); | ||
} | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
describe('with a custom createConsumerLogger function', () => { | ||
let mockConsumerLogger: Stub<Logger>; | ||
let mockCreateConsumerLogger: jest.Mock<Stub<Logger>>; | ||
|
||
beforeEach(() => { | ||
mockConsumerLogger = { | ||
trace: jest.fn(), | ||
debug: jest.fn(), | ||
info: jest.fn(), | ||
warn: jest.fn(), | ||
error: jest.fn() | ||
}; | ||
|
||
mockCreateConsumerLogger = jest.fn(() => mockConsumerLogger); | ||
|
||
loggerDefinition = defineLogger(mockCreateConsumerLogger); | ||
|
||
logger = loggerDefinition.create(mockEnv)['1.0.0']('test:id') | ||
.featureService; | ||
}); | ||
|
||
it('calls the given createConsumerLogger with the consumerUid', () => { | ||
expect(mockCreateConsumerLogger.mock.calls).toEqual([['test:id']]); | ||
}); | ||
|
||
it('uses the defined consumer logger as Feature Service', () => { | ||
expect(logger).toBe(mockConsumerLogger); | ||
}); | ||
}); | ||
}); | ||
}); |
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,28 @@ | ||
import { | ||
FeatureServiceBinder, | ||
FeatureServiceProviderDefinition, | ||
Logger, | ||
SharedFeatureService | ||
} from '@feature-hub/core'; | ||
|
||
export {Logger} from '@feature-hub/core'; | ||
|
||
export interface SharedLogger extends SharedFeatureService { | ||
readonly '1.0.0': FeatureServiceBinder<Logger>; | ||
} | ||
|
||
export type ConsumerLoggerCreator = (consumerUid: string) => Logger; | ||
|
||
export function defineLogger( | ||
createConsumerLogger: ConsumerLoggerCreator = () => console | ||
): FeatureServiceProviderDefinition<SharedLogger> { | ||
return { | ||
id: 's2:logger', | ||
|
||
create: () => ({ | ||
'1.0.0': consumerUid => ({ | ||
featureService: createConsumerLogger(consumerUid) | ||
}) | ||
}) | ||
}; | ||
} |
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,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"paths": {}, | ||
"outDir": "lib/cjs", | ||
"rootDir": "src" | ||
} | ||
} |
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 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "esnext", | ||
"paths": {}, | ||
"outDir": "lib/esm", | ||
"rootDir": "src" | ||
} | ||
} |
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,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*"] | ||
} |
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,3 @@ | ||
{ | ||
"extends": "../../tslint.json" | ||
} |