Skip to content

Commit

Permalink
Merge c25b73e into 2201ac5
Browse files Browse the repository at this point in the history
  • Loading branch information
juneil committed Jul 31, 2017
2 parents 2201ac5 + c25b73e commit 13a658c
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 190 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.1.3
8.2.1
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@
### `yarn` or `npm` it in your `package.json`

```bash
$ npm install --save @hapiness/logger
$ npm install --save @hapiness/core @hapiness/logger

or

$ yarn add @hapiness/logger
$ yarn add @hapiness/core @hapiness/logger
```

```javascript
"dependencies": {
"@hapiness/core": "^1.0.0-rc.6",
"@hapiness/logger": "^1.0.0-rc.6",
"@hapiness/logger": "^1.0.0-rc.6.1",
//...
}
//...
Expand Down Expand Up @@ -102,6 +102,23 @@ Hapiness.bootstrap(HapinessModuleApp, [ LoggerExt.setConfig({ logger: myLogger }

```

### Access logs

If HttpServerExt is loaded, the LoggerModule will inject an AccessLogs component.
To desactivate the access logs, provide a module's config:
```javascript
@HapinessModule({
version: '1.0.0',
imports: [
LoggerModule.setConfig({ accessLogs: false })
]
})
class HapinessModuleApp {
...
}
```


[Back to top](#table-of-contents)

## Contributing
Expand All @@ -119,6 +136,8 @@ To set up your development environment:

## Change History

* v1.0.0-rc.6.1 (2017-07-31)
* Access logs
* v1.0.0-rc.6 (2017-07-17)
* Latest packages' versions.
* Update dependencies declarations.
Expand Down
182 changes: 92 additions & 90 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,96 +1,98 @@
{
"name": "@hapiness/logger",
"version": "1.0.0-rc.6.1",
"description": "Hapiness module for logging",
"main": "index.js",
"types": "index.d.ts",
"private": false,
"scripts": {
"test": "make test",
"pretest": "make pretest",
"coveralls": "make coveralls",
"packaging": "make packaging",
"prebuild": "make pretest && make test && make clean",
"build": "make tsc",
"postbuild": "make packaging"
"name": "@hapiness/logger",
"version": "1.0.0-rc.6.1",
"description": "Hapiness module for logging",
"main": "index.js",
"types": "index.d.ts",
"private": false,
"scripts": {
"test": "make test",
"pretest": "make pretest",
"coveralls": "make coveralls",
"packaging": "make packaging",
"prebuild": "make pretest && make test && make clean",
"build": "make tsc",
"postbuild": "make packaging"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/hapinessjs/logger-module.git"
},
"keywords": [
"Logger",
"Log",
"Hapiness",
"Framework",
"NodeJS",
"Node",
"Logger",
"Log",
"Async",
"Decorator",
"RxJS",
"Rx",
"ReactiveX",
"Observable",
"Observer",
"Module",
"ES2015",
"ES2016",
"ES2017",
"ES6",
"ES7",
"ES8",
"Typescript"
],
"contributors": [
{
"name": "Julien Fauville",
"url": "https://github.com/Juneil"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/hapinessjs/logger-module.git"
{
"name": "Antoine Gomez",
"url": "https://github.com/antoinegomez"
},
"keywords": [
"Logger",
"Log",
"Hapiness",
"Framework",
"NodeJS",
"Node",
"Logger",
"Log",
"Async",
"Decorator",
"RxJS",
"Rx",
"ReactiveX",
"Observable",
"Observer",
"Module",
"ES2015",
"ES2016",
"ES2017",
"ES6",
"ES7",
"ES8",
"Typescript"
],
"contributors": [
{
"name": "Julien Fauville",
"url": "https://github.com/Juneil"
},
{
"name": "Antoine Gomez",
"url": "https://github.com/antoinegomez"
},
{
"name": "Sébastien Ritz",
"url": "https://github.com/reptilbud"
},
{
"name": "Nicolas Jessel",
"url": "https://github.com/njl07"
}
],
"license": "SEE LICENSE IN https://github.com/hapinessjs/logger-module/blob/master/LICENSE.md",
"bugs": {
"url": "https://github.com/hapinessjs/logger-module/issues"
{
"name": "Sébastien Ritz",
"url": "https://github.com/reptilbud"
},
"homepage": "https://github.com/hapinessjs/logger-module#readme",
"dependencies": {
"@types/node": "^8.0.13",
"debug": "^2.6.8"
},
"devDependencies": {
"@hapiness/core": "^1.0.0-rc.6",
"@types/fs-extra": "^3.0.3",
"coveralls": "^2.13.1",
"fs-extra": "^4.0.0",
"intercept-stdout": "^0.1.2",
"istanbul": "^1.1.0-alpha.1",
"mocha": "^3.4.2",
"mocha-typescript": "^1.1.7",
"rimraf": "^2.6.1",
"rxjs": "^5.4.2",
"ts-node": "^3.2.0",
"tslint": "^5.5.0",
"typescript": "^2.4.1",
"unit.js": "^2.0.0"
},
"engines": {
"node": ">=7.0.0"
},
"peerDependencies": {
"@hapiness/core": "^1.0.0-rc.6",
"rxjs": "^5.4.2"
{
"name": "Nicolas Jessel",
"url": "https://github.com/njl07"
}
],
"license": "SEE LICENSE IN https://github.com/hapinessjs/logger-module/blob/master/LICENSE.md",
"bugs": {
"url": "https://github.com/hapinessjs/logger-module/issues"
},
"homepage": "https://github.com/hapinessjs/logger-module#readme",
"dependencies": {
"@types/chalk": "^0.4.31",
"@types/node": "^8.0.17",
"chalk": "^2.0.1",
"debug": "^2.6.8"
},
"devDependencies": {
"@hapiness/core": "^1.0.0-rc.6",
"@types/fs-extra": "^4.0.0",
"coveralls": "^2.13.1",
"fs-extra": "^4.0.0",
"intercept-stdout": "^0.1.2",
"istanbul": "^1.1.0-alpha.1",
"mocha": "^3.4.2",
"mocha-typescript": "^1.1.7",
"rimraf": "^2.6.1",
"rxjs": "^5.4.2",
"ts-node": "^3.3.0",
"tslint": "^5.5.0",
"typescript": "^2.4.2",
"unit.js": "^2.0.0"
},
"engines": {
"node": ">=7.0.0"
},
"peerDependencies": {
"@hapiness/core": "^1.0.0-rc.6",
"rxjs": "^5.4.2"
}
}
7 changes: 7 additions & 0 deletions src/module/logger.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { InjectionToken } from '@hapiness/core';

export const LOGGER_CONFIG = new InjectionToken('logger_config');

export interface LoggerConfig {
accessLogs: boolean
}
80 changes: 80 additions & 0 deletions src/module/logger.lifecycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import {
Optional,
Inject,
Lifecycle,
OnEvent,
Request,
ReplyWithContinue
} from '@hapiness/core';
import { LoggerService } from './logger.service';
import { LOGGER_CONFIG, LoggerConfig } from './logger.config';
import * as Chalk from 'chalk';

@Lifecycle({
event: 'onPreResponse'
})
export class AccessLogs implements OnEvent {

constructor(
@Optional() @Inject(LOGGER_CONFIG) private config: LoggerConfig,
private logger: LoggerService
) {
this.config = this.config || { accessLogs: true };
}

onEvent(request: Request, reply: ReplyWithContinue) {
if (!this.config.accessLogs) {
reply.continue();
return;
}
const res = request.raw.res;
const data = {
method: request.method,
path: request.path,
query: request.query,
params: request.params,
statusCode: res.statusCode
};
this.logger.info(
`${this.getMethod(request.method)} ${request.path} ${this.getStatus(res.statusCode)}`,
data
);
reply.continue();
}

/**
* Get formatted status
*
* @param {number} status
*/
private getStatus(status: number): string {
/* istanbul ignore if */
if (status >= 400) {
return Chalk.red(status.toString());
} else {
return Chalk.green(status.toString());
}
}

/**
* Get formatted method
*
* @param {string} method
*/
private getMethod(method: string): string {
const _method = method.toUpperCase();
/* istanbul ignore next */
switch (_method) {
case 'GET':
return Chalk.green.bold(_method);
case 'POST':
return Chalk.yellow.bold(_method);
case 'PUT':
return Chalk.blue.bold(_method);
case 'DELETE':
return Chalk.red.bold(_method);
default:
return Chalk.cyan.bold(_method);
}
}
}
17 changes: 14 additions & 3 deletions src/module/logger.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { HapinessModule } from '@hapiness/core';
import { HapinessModule, CoreModuleWithProviders } from '@hapiness/core';
import { LoggerService } from './logger.service';
import { AccessLogs } from './logger.lifecycle';
import { LOGGER_CONFIG, LoggerConfig } from './logger.config';

@HapinessModule({
version: '1.0.0-rc.6',
version: '1.0.0-rc.7',
declarations: [ AccessLogs ],
providers: [ LoggerService ],
exports: [ LoggerService ]
})
export class LoggerModule {}
export class LoggerModule {
static setConfig(config: LoggerConfig): CoreModuleWithProviders {
return {
module: LoggerModule,
providers: [{ provide: LOGGER_CONFIG, useValue: config }]
};
}
}
2 changes: 1 addition & 1 deletion src/module/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class LoggerService {

private logger;

constructor(@Optional() @Inject(LoggerExt) loggerExt) {
constructor(@Optional() @Inject(LoggerExt) loggerExt?) {
this.logger = loggerExt ? loggerExt : console;
}

Expand Down

0 comments on commit 13a658c

Please sign in to comment.