-
Notifications
You must be signed in to change notification settings - Fork 16
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 a decorator to log a class method (#798)
- Loading branch information
Showing
10 changed files
with
116 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
{ | ||
"extends": ["../../.eslintrc.js", "../../.eslintrc.angular.js"], | ||
"ignorePatterns": ["!**/*"] | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["log-method.decorator.ts"], | ||
"rules": { | ||
"no-console": "off" | ||
} | ||
} | ||
] | ||
} |
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 +1 @@ | ||
export * from './lib/decorators'; | ||
export * from './lib'; |
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 +1,2 @@ | ||
export * from './log-method/log-method.decorator'; | ||
export * from './track-changes/track-changes.decorator'; |
63 changes: 63 additions & 0 deletions
63
libs/client-util-decorators/src/lib/decorators/log-method/log-method.decorator.spec.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,63 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { ComponentFixture, TestBed, TestModuleMetadata } from '@angular/core/testing'; | ||
|
||
import { logMethod } from './log-method.decorator'; | ||
|
||
@Component({ | ||
selector: 'app-log-method-testing-component', | ||
template: `<div></div>`, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
class AppLogMethodTestingComponent { | ||
@logMethod(true) | ||
public logEnabled(options: { test: string }) { | ||
return options; | ||
} | ||
|
||
@logMethod() | ||
public logEnabledImplicitly(options: { test: string }) { | ||
return options; | ||
} | ||
|
||
@logMethod(false) | ||
public logDisabled(options: { test: string }) { | ||
return options; | ||
} | ||
} | ||
|
||
describe('trackChanges', () => { | ||
const testBedConfig: TestModuleMetadata = { | ||
declarations: [AppLogMethodTestingComponent], | ||
}; | ||
|
||
let fixture: ComponentFixture<AppLogMethodTestingComponent>; | ||
let component: AppLogMethodTestingComponent; | ||
|
||
let spy: jest.SpyInstance; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule(testBedConfig).compileComponents(); | ||
fixture = TestBed.createComponent(AppLogMethodTestingComponent); | ||
component = fixture.componentInstance; | ||
spy = jest.spyOn(console, 'log'); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should log changes if attached and enabled explicitly', () => { | ||
component.logEnabled({ test: 'test' }); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should log changes if attached and enabled implicitly', () => { | ||
component.logEnabledImplicitly({ test: 'test' }); | ||
expect(spy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should not log changes if attached and disabled', () => { | ||
component.logDisabled({ test: 'test' }); | ||
expect(spy).not.toHaveBeenCalled(); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
libs/client-util-decorators/src/lib/decorators/log-method/log-method.decorator.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,20 @@ | ||
/** | ||
* @title The log method decorator. | ||
* @description This log is mainly useful for development and should not be used in production. | ||
* @param enable Indicates whether to enable the logger or not. | ||
* @param propertyKey The class property key. | ||
*/ | ||
export function logMethod<TargetClass extends object = Record<string, unknown>>(enable = true) { | ||
return function (targetClass: TargetClass, propertyKey: string, descriptor: PropertyDescriptor) { | ||
const originalMethod: (...args: any[]) => unknown = descriptor.value; | ||
descriptor.value = function (...args: any[]) { | ||
const className = targetClass.constructor.name; | ||
const result = originalMethod.apply(this, args); | ||
if (enable) { | ||
console.log(`Executing: ${className}.${propertyKey}`, `\nArguments: ${JSON.stringify(args)}`, `\nResult: ${result}`); | ||
} | ||
return result; | ||
}; | ||
return descriptor; | ||
}; | ||
} |
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 @@ | ||
export * from './decorators'; |
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,4 +1,12 @@ | ||
{ | ||
"extends": ["../.eslintrc.js"], | ||
"ignorePatterns": ["!**/*"] | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*logger*.ts"], | ||
"rules": { | ||
"no-console": "off" | ||
} | ||
} | ||
] | ||
} |