Skip to content

Commit

Permalink
feat: implement provideLumberjackCustomDrivers to allow the angular c…
Browse files Browse the repository at this point in the history
…lient to use agnostic drivers
  • Loading branch information
NachoVazquez committed Aug 9, 2023
1 parent 771815b commit 2c7f384
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/examples/lumberjack-app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { inject, InjectionToken } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';

import { provideLumberjackAngularHttpDriver, withHttpOptions } from '@lumberjackjs/angular-http-driver';
import { provideLumberjack } from '@lumberjackjs/angular';
import { LumberjackLog, LumberjackOptions } from '@lumberjackjs/core';
import { provideLumberjack, provideLumberjackCustomDrivers } from '@lumberjackjs/angular';
import { LumberjackLevel, LumberjackLog, LumberjackOptions } from '@lumberjackjs/core';
import { LumberjackConsoleDriver } from '@lumberjackjs/core/console-driver';

import { AppComponent } from './app/app.component';

Expand All @@ -20,13 +21,16 @@ const cypressLumberjackOptions: LumberjackOptions = {

const easyToken = new InjectionToken('easy-provider');

const consoleDriver = new LumberjackConsoleDriver({ levels: [LumberjackLevel.Verbose] });

bootstrapApplication(AppComponent, {
providers: [
{
provide: easyToken,
useValue: 'provider-easy',
},
provideLumberjack('Cypress' in window ? cypressLumberjackOptions : undefined),
provideLumberjackCustomDrivers(consoleDriver),
provideLumberjackAngularHttpDriver(
withHttpOptions({
origin: 'ForestApp',
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/lumberjack-app/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@lumberjackjs/angular": ["dist/packages/lumberjackjs/angular"],
"@lumberjackjs/angular-http-driver": ["dist/packages/lumberjackjs/angular-http-driver"],
"@lumberjackjs/core": ["dist/packages/lumberjackjs/core"],
"@lumberjackjs/core/console-driver": ["dist/packages/lumberjackjs/core/console-driver"]
"@lumberjackjs/core/console-driver": ["dist/packages/lumberjackjs/core/console-driver/src"]
}
},
"files": ["src/main.ts"],
Expand Down
7 changes: 7 additions & 0 deletions packages/lumberjackjs/angular/src/configuration-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
LumberjackModule,
LumberjackRootModule,
provideLumberjack,
provideLumberjackCustomDrivers,
} from './index';

describe('Configuration API', () => {
Expand All @@ -31,6 +32,12 @@ describe('Configuration API', () => {

expect(isFunction(sut)).toBeTruthy();
});

it(`exposes ${provideLumberjackCustomDrivers.name}`, () => {
const sut = provideLumberjackCustomDrivers;

expect(isFunction(sut)).toBeTruthy();
});
});

describe('Dependency injection tokens', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/lumberjackjs/angular/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { lumberjackLogDriverConfigToken } from './lib/configuration/lumberjack-l
export { LumberjackModule } from './lib/configuration/lumberjack.module';
export { LumberjackRootModule } from './lib/configuration/lumberjack-root.module';
export { provideLumberjack } from './lib/configuration/provide-lumberjack';
export { provideLumberjackCustomDrivers } from './lib/configuration/provide-custom-drivers';

// Logging
export { LumberjackLogger } from './lib/logging/lumberjack-logger.service';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { TestBed } from '@angular/core/testing';

import { createNoopDriver } from '@internal/core/test-util';
import { LumberjackLevel } from '@lumberjackjs/core';
import { LumberjackConsoleDriver } from '@lumberjackjs/core/console-driver';

import { lumberjackLogDriverToken } from '../log-drivers/lumberjack-log-driver.token';

import { provideLumberjackCustomDrivers } from './provide-custom-drivers';

describe(provideLumberjackCustomDrivers.name, () => {
const consoleDriver = new LumberjackConsoleDriver({ levels: [LumberjackLevel.Verbose] });
const noopDriver = createNoopDriver({ levels: [LumberjackLevel.Verbose] });

it('provides the custom driver', () => {
TestBed.configureTestingModule({
providers: [provideLumberjackCustomDrivers(consoleDriver)],
});
const customDriver = TestBed.inject(lumberjackLogDriverToken);

expect(customDriver).toBeDefined();
expect(customDriver).toEqual([consoleDriver]);
});

it('provides the multiple custom drivers', () => {
TestBed.configureTestingModule({
providers: [provideLumberjackCustomDrivers([consoleDriver, noopDriver])],
});
const customDrivers = TestBed.inject(lumberjackLogDriverToken);

expect(customDrivers).toBeDefined();
expect(customDrivers.length).toBe(2);
expect(customDrivers).toEqual([consoleDriver, noopDriver]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Provider } from '@angular/core';

import { LumberjackLogDriver } from '@lumberjackjs/core';

import { lumberjackLogDriverToken } from '../log-drivers/lumberjack-log-driver.token';

/**
* Returns the [dependency-injection providers](https://angular.io/guide/glossary#provider)
*
* for one or more custom/agnostic `LumberjackDriver` and its `LumberjackDriverConfig`.
* @usageNotes
*
* The function is useful when you want to bootstrap an application using
* the `bootstrapApplication` function and want to make available the `LumberjackDriver` providers.
*
* ```typescript
* bootstrapApplication(RootComponent, {
* providers: [
* provideLumberjack({...}),
* provideLumberjackCustomDriver({...})
* ]
* });
* ```
*
* @publicApi
*/
export function provideLumberjackCustomDrivers<Driver extends LumberjackLogDriver>(driver: Driver): Provider[];
export function provideLumberjackCustomDrivers<Driver extends LumberjackLogDriver>(drivers: Driver[]): Provider[];

export function provideLumberjackCustomDrivers<Driver extends LumberjackLogDriver>(
drivers: Driver | Driver[]
): Provider[] {
if (Array.isArray(drivers)) {
return drivers.map((driver) => ({
provide: lumberjackLogDriverToken,
useValue: driver,
multi: true,
}));
}

return [
{
provide: lumberjackLogDriverToken,
useValue: drivers,
multi: true,
},
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { LumberjackLogDriver } from '@lumberjackjs/core';
/**
* A multi-provider token which log drivers use to register with Lumberjack.
*/
export const lumberjackLogDriverToken: InjectionToken<LumberjackLogDriver> = new InjectionToken(
export const lumberjackLogDriverToken: InjectionToken<LumberjackLogDriver[]> = new InjectionToken(
'__LUMBERJACK_LOG_DRIVER_TOKEN__'
);

0 comments on commit 2c7f384

Please sign in to comment.