-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
keenondrums
committed
Apr 7, 2019
1 parent
553b2cb
commit d780ab7
Showing
6 changed files
with
166 additions
and
11 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 |
---|---|---|
@@ -1,10 +1,126 @@ | ||
# class-logger | ||
|
||
Boilerplate-free decorator-based class logging. Log method calls and creation of your class easily with a help of two decorators. No prototype mutation. Highly configurable. Built with TypeScript. | ||
|
||
```ts | ||
@LogClass() | ||
class Test { | ||
@Log() | ||
method1() { | ||
return 123 | ||
} | ||
} | ||
``` | ||
|
||
Logs `Test.construct. Args: [].` before a class instance is created. | ||
Logs `Test.method1. Args: [].` before the method call. | ||
Logs `Test.method1 -> done. Args: []. Res: 123.` after it. | ||
|
||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
|
||
**Table of Contents** _generated with [DocToc](https://github.com/thlorenz/doctoc)_ | ||
|
||
- [class-logger](#class-logger) | ||
- [Installation](#installation) | ||
- [Quick start](#quick-start) | ||
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
||
## Installation | ||
|
||
1. Run | ||
|
||
``` | ||
npm i class-logger reflect-metadata | ||
``` | ||
|
||
2. If you use TypeScript set in you tsconfig.json | ||
|
||
```json | ||
{ | ||
"compilerOptions": { | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true | ||
} | ||
} | ||
``` | ||
|
||
3. If you use JavaScript configure your babel to support decorators and class properties | ||
4. At the top of your project root file add | ||
|
||
```ts | ||
import 'reflect-metadata' | ||
``` | ||
|
||
## Quick start | ||
|
||
You can log: | ||
|
||
- Class construction | ||
- Prototype and static method calls, both: synchronous and asynchronous. Any thrown errors are properly logged and re-thrown. | ||
- Own and static property calls if those properties return functions (synchronous or asynchronous). Error handling is the same as for method calls. | ||
|
||
```ts | ||
import { LogClass, Log } from 'class-logger' | ||
|
||
@LogClass() | ||
class Test { | ||
@Log() | ||
method1() { | ||
return 123 | ||
} | ||
|
||
@Log() | ||
async methodAsync1() { | ||
// do something asynchronous | ||
return Symbol() | ||
} | ||
|
||
@Log() | ||
methodError() { | ||
throw new Error() | ||
} | ||
|
||
@Log() | ||
property1 = () => null | ||
|
||
@Log() | ||
static methodStatic1(arg1) { | ||
return { | ||
prop1: 'test', | ||
} | ||
} | ||
} | ||
|
||
// Logs to the console before the method call: | ||
// 'Test.methodStatic1. Args: [42].' | ||
Test.methodStatic1(42) | ||
// Logs to the console after the method call: | ||
// 'Test.methodStatic1 -> done. Args: [42]. Res: {"prop1":"test"}.' | ||
|
||
// Logs to the console before the class' construction: | ||
// 'Test.construct. Args: [].' | ||
const test = new Test() | ||
|
||
// Logs to the console before the method call: | ||
// 'Test.method1. Args: [].' | ||
test.method1() | ||
// Logs to the console after the method call: | ||
// 'Test.method1 -> done. Args: []. Res: 123.' | ||
|
||
// Logs to the console before the method call: | ||
// 'Test.methodAsync1. Args: [].' | ||
test.methodAsync1() | ||
// Logs to the console after the method call (after the promise is resolved): | ||
// 'Test.methodAsync1 -> done. Args: []. Res: Symbol().' | ||
|
||
// Logs to the console before the method call: | ||
// 'Test.methodError. Args: [].' | ||
test.methodError() | ||
// Logs to the console after the method call: | ||
// 'Test.methodError -> error. Args: []. Res: {"className":"Error","name":"Error","message":"","stack":"some stack trace"}.' | ||
|
||
// Logs to the console before the method call: | ||
// 'Test.property1. Args: [].' | ||
test.property1() | ||
// Logs to the console after the method call: | ||
// 'Test.property1 -> done. Args: []. Res: null.' | ||
``` |
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
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
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