Skip to content

Commit

Permalink
fix: rebase issues
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoVazquez committed Jun 9, 2023
1 parent fea851a commit 49cf544
Show file tree
Hide file tree
Showing 19 changed files with 1,189 additions and 886 deletions.
3 changes: 2 additions & 1 deletion packages/examples/lumberjack-app/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { withInterceptors } from '@angular/common/http';
import { inject, InjectionToken } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';

import { LumberjackLog, LumberjackOptions, provideLumberjack } from '@ngworker/lumberjack';
import { provideLumberjack } from '@ngworker/lumberjack';
import { provideLumberjackConsoleDriver } from '@ngworker/lumberjack/console-driver';
import { provideLumberjackHttpDriver, withHttpOptions } from '@ngworker/lumberjack/http-driver';
import { LumberjackLog, LumberjackOptions } from '@webworker/lumberjack';

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

Expand Down
8 changes: 0 additions & 8 deletions packages/internal/angular/test-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,5 @@ export * from './lib/object-driver/object-driver.module';
export * from './lib/object-driver/object-driver-root.module';
export * from './lib/object-driver/object.service';

// Type checking
export * from './lib/type-checking/is-class';
export * from './lib/type-checking/is-object';
export * from './lib/type-checking/is-function';

// Time
export * from './lib/time/fake-time.service';

// Types
export * from './lib/types/writable';
4 changes: 3 additions & 1 deletion packages/internal/core/test-util/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Spy driver
export * from './lib/spy-driver/create-spy-driver';
export * from './lib/spy-driver/spy-driver.config';

// Noop driver
export * from './lib/noop-driver/create-noop-driver';
export * from './lib/noop-driver/noop-driver.config';
Expand All @@ -24,6 +23,9 @@ export * from './lib/type-checking/is-class';
export * from './lib/type-checking/is-object';
export * from './lib/type-checking/is-function';

// Types
export * from './lib/types/writable';

// Time
export * from './lib/time/create-fake-time';

Expand Down
3 changes: 3 additions & 0 deletions packages/internal/core/test-util/src/lib/types/writable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Writable<TObject> = {
-readonly [Key in keyof TObject]: TObject[Key];
} & Record<string, unknown>;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isClass } from '@internal/core/test-util';
import { isClass, isFunction } from '@internal/core/test-util';

import {
LumberjackConsoleDriverModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { TestBed } from '@angular/core/testing';

import {
lumberjackConfigToken,
LumberjackLevel,
LumberjackLogDriver,
LumberjackLogDriverConfig,
lumberjackLogDriverToken,
provideLumberjack,
} from '@ngworker/lumberjack';
import { lumberjackConfigToken, lumberjackLogDriverToken, provideLumberjack } from '@ngworker/lumberjack';
import { LumberjackLevel, LumberjackLogDriver, LumberjackLogDriverConfig } from '@webworker/lumberjack';

import { LumberjackConsoleDriver } from '../log-drivers/lumberjack-console.driver';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Provider } from '@angular/core';

import {
LumberjackLogDriverConfig,
lumberjackLogDriverConfigToken,
lumberjackLogDriverToken,
} from '@ngworker/lumberjack';
import { lumberjackLogDriverConfigToken, lumberjackLogDriverToken } from '@ngworker/lumberjack';
import { LumberjackLogDriverConfig } from '@webworker/lumberjack';

import { LumberjackConsoleDriver } from '../log-drivers/lumberjack-console.driver';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import { TestBed } from '@angular/core/testing';
import { HttpHandlerFn, HttpRequest, withInterceptors } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';

import {
LumberjackConfigLevels,
LumberjackLevel,
LumberjackLogDriver,
lumberjackLogDriverToken,
provideLumberjack,
} from '@ngworker/lumberjack';
import { createDriverLog, Writable } from '@internal/test-util';
import { createDriverLog, createFakeTime, Writable } from '@internal/core/test-util';
import { lumberjackLogDriverToken, provideLumberjack } from '@ngworker/lumberjack';
import { LumberjackConfigLevels, LumberjackLevel, LumberjackLogDriver } from '@webworker/lumberjack';

import { LumberjackHttpDriver } from '../log-drivers/lumberjack-http.driver';

import { LumberjackHttpDriverInternalConfig } from './lumberjack-http-driver-internal.config';
import { LumberjackHttpDriverConfig } from './lumberjack-http-driver.config';
import { LumberjackHttpDriverOptions } from './lumberjack-http-driver.options';
import {
HttpClientFeatures,
provideLumberjackHttpDriver,
withHttpConfig,
withHttpOptions,
} from './provide-lumberjack-http-driver';
import { LumberjackHttpDriverInternalConfig } from './lumberjack-http-driver-internal.config';
import { LumberjackHttpDriverOptions } from './lumberjack-http-driver.options';

function createHttpOptions(
extraOptions: { levels?: LumberjackConfigLevels; identifier?: string } = {}
Expand Down Expand Up @@ -99,6 +94,8 @@ const createHttpDriverWithOptions = (
return httpDriver;
};

const fakeTime = createFakeTime();

describe(provideLumberjackHttpDriver.name, () => {
it('provides the HTTP driver', () => {
const httpDriver = createHttpDriver();
Expand Down Expand Up @@ -142,7 +139,13 @@ describe(provideLumberjackHttpDriver.name, () => {
const config = createHttpConfig([LumberjackLevel.Error]);
const features: HttpClientFeatures = [withInterceptors([testInterceptor])];
const httpDriver = createHttpDriver({ config, features });
const log = createDriverLog(LumberjackLevel.Info, LumberjackLevel.Info, '', 'Test Log');
const log = createDriverLog(
fakeTime.getUnixEpochTicks.bind(fakeTime),
LumberjackLevel.Info,
LumberjackLevel.Info,
'',
'Test Log'
);

httpDriver.logInfo(log);

Expand Down Expand Up @@ -229,7 +232,13 @@ describe(provideLumberjackHttpDriver.name, () => {
const options = createHttpOptions({ levels: customLevels });

const httpDriver = createHttpDriverWithOptions({ options, features });
const log = createDriverLog(LumberjackLevel.Info, LumberjackLevel.Info, '', 'Test Log');
const log = createDriverLog(
fakeTime.getUnixEpochTicks.bind(fakeTime),
LumberjackLevel.Info,
LumberjackLevel.Info,
'',
'Test Log'
);

httpDriver.logInfo(log);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { provideHttpClient } from '@angular/common/http';
import { EnvironmentProviders, makeEnvironmentProviders, Provider } from '@angular/core';

import { LumberjackLogDriverConfig, lumberjackLogDriverConfigToken } from '@ngworker/lumberjack';
import { lumberjackLogDriverConfigToken } from '@ngworker/lumberjack';
import { LumberjackLogDriverConfig } from '@webworker/lumberjack';

import { LumberjackHttpDriver } from '../log-drivers/lumberjack-http.driver';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ import { HttpTestingController, provideHttpClientTesting, TestRequest } from '@a
import { TestBed } from '@angular/core/testing';
import { VERSION } from '@angular/platform-browser';

import { repeatSideEffect } from '@internal/core/test-util';
import { createCriticalDriverLog, createDriverLog, createFakeTime } from '@internal/core/test-util';
import { lumberjackLogDriverToken, LumberjackModule } from '@ngworker/lumberjack';
import { createCriticalDriverLog, createDriverLog, createFakeTime, repeatSideEffect } from '@internal/core/test-util';
import { lumberjackLogDriverToken, provideLumberjack } from '@ngworker/lumberjack';
import {
LumberjackLevel,
LumberjackLogDriver,
LumberjackLogDriverLog,
LumberjackLogLevel,
LumberjackLogPayload,
provideLumberjack,
} from '@ngworker/lumberjack';
} from '@webworker/lumberjack';

import { LumberjackHttpDriverOptions } from '../configuration/lumberjack-http-driver.options';
import { provideLumberjackHttpDriver, withHttpOptions } from '../configuration/provide-lumberjack-http-driver';
Expand Down
2 changes: 1 addition & 1 deletion packages/ngworker/lumberjack/src/configuration-api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InjectionToken } from '@angular/core';

import { isClass } from '@internal/core/test-util';
import { isClass, isFunction } from '@internal/core/test-util';

import {
lumberjackConfigToken,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { inject, NgModule } from '@angular/core';

import { configFactory, LumberjackConfig, LumberjackLogDriverConfig } from '@webworker/lumberjack';

/**
* Do not import directly. Use `LumberjackModule.forRoot`.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ModuleWithProviders, NgModule } from '@angular/core';
import { LumberjackOptions } from '@webworker/lumberjack';

import { LumberjackRootModule } from './lumberjack-root.module';
import { LumberjackOptions } from './lumberjack.options';
import { provideLumberjack } from './provide-lumberjack';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { TestBed } from '@angular/core/testing';

import {
defaultDevelopmentLevels,
defaultProductionLevels,
LumberjackConfig,
lumberjackFormatLog,
LumberjackLevel,
LumberjackLogDriverConfig,
LumberjackOptions,
} from '@webworker/lumberjack';

import { isProductionEnvironmentToken } from '../environment/is-production-environment.token';
import { lumberjackFormatLog } from '../formatting/lumberjack-format-log';
import { LumberjackService } from '../logging/lumberjack.service';
import { LumberjackLevel } from '../logs/lumberjack-level';

import { defaultDevelopmentLevels } from './default-development-levels';
import { defaultProductionLevels } from './default-production-levels';
import { lumberjackConfigToken } from './lumberjack-config.token';
import { lumberjackLogDriverConfigToken } from './lumberjack-log-driver-config.token';
import { LumberjackLogDriverConfig } from './lumberjack-log-driver.config';
import { LumberjackConfig } from './lumberjack.config';
import { LumberjackOptions } from './lumberjack.options';
import { provideLumberjack } from './provide-lumberjack';

describe(provideLumberjack.name, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { Provider } from '@angular/core';

import {
defaultDevelopmentLevels,
defaultProductionLevels,
LumberjackConfig,
lumberjackFormatLog,
LumberjackLogDriverConfig,
LumberjackOptions,
} from '@webworker/lumberjack';

import { isProductionEnvironmentToken } from '../environment/is-production-environment.token';
import { lumberjackFormatLog } from '../formatting/lumberjack-format-log';
import { LumberjackLogFormatter } from '../formatting/lumberjack-log-formatter.service';
import { LumberjackLogDriverLogger } from '../log-drivers/lumberjack-log-driver-logger';
import { LumberjackLogFactory } from '../logging/lumberjack-log-factory';
import { LumberjackLogFormatterService } from '../formatting/lumberjack-log-formatter.service';
import { LumberjackService } from '../logging/lumberjack.service';
import { LumberjackTimeService } from '../time/lumberjack-time.service';

import { defaultDevelopmentLevels } from './default-development-levels';
import { defaultProductionLevels } from './default-production-levels';
import { lumberjackConfigToken } from './lumberjack-config.token';
import { lumberjackLogDriverConfigToken } from './lumberjack-log-driver-config.token';
import { LumberjackLogDriverConfig } from './lumberjack-log-driver.config';
import { lumberjackOptionsToken } from './lumberjack-options.token';
import { LumberjackConfig } from './lumberjack.config';
import { LumberjackOptions } from './lumberjack.options';

export function configFactory(isProductionEnvironment: boolean, options: LumberjackOptions = {}): LumberjackConfig {
return {
Expand Down Expand Up @@ -64,9 +65,7 @@ export function provideLumberjack(options?: LumberjackOptions): Provider[] {
useFactory: logDriverConfigFactory,
},
{ provide: LumberjackService, useClass: LumberjackService },
LumberjackLogFormatter,
LumberjackLogDriverLogger,
LumberjackLogFactory,
LumberjackLogFormatterService,
LumberjackTimeService,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class LumberjackLogFormatterService<TPayload extends LumberjackLogPayload

readonly #lumberjackLogFormatter = createLumberjackLogFormatter<TPayload>({
config: this.#config,
getUnixEpochTicks: this.#time.getUnixEpochTicks.bind(this.time),
getUnixEpochTicks: this.#time.getUnixEpochTicks.bind(this.#time),
});

formatLog(log: LumberjackLog<TPayload>): LumberjackLogFormatterResult<TPayload> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ export class LumberjackService<TPayload extends LumberjackLogPayload | void = vo
* The registered log drivers.
*/
readonly #drivers = inject<LumberjackLogDriver<TPayload>[]>(lumberjackLogDriverToken, { optional: true }) ?? [];
readonly #driverLogger = inject<LumberjackLogDriverLogger<TPayload>>(LumberjackLogDriverLogger);
readonly #logFormatter = inject<LumberjackLogFormatter<TPayload>>(LumberjackLogFormatter);
readonly #driverLogger = lumberjackLogDriverLoggerFactory<TPayload>();
readonly #logFormatter = inject<LumberjackLogFormatterService<TPayload>>(LumberjackLogFormatterService);
readonly #time = inject(LumberjackTimeService);

readonly #lumberjack = createLumberjack<TPayload>({
logFormatter: this.#logFormatter.formatLog.bind(this.logFormatter),
driverLogger: this.#driverLogger.log.bind(this.driverLogger),
logFormatter: this.#logFormatter,
logDriverLogger: this.#driverLogger,
drivers: this.#drivers,
getUnixEpochTicks: this.#time.getUnixEpochTicks.bind(this.time),
getUnixEpochTicks: this.#time.getUnixEpochTicks.bind(this.#time),
});

/**
Expand Down
18 changes: 10 additions & 8 deletions packages/webworker/lumberjack/src/lib/logging/create-lumberjack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@ interface LumberjackDependencies<TPayload extends LumberjackLogPayload | void> {
drivers: LumberjackLogDriver<TPayload>[];
logFormatter: LumberjackLogFormatter<TPayload>;
logDriverLogger: LumberjackLogDriverLogger<TPayload>;
// TODO: this should be optional and it should use the () => new Date().valueOf() or something like that
getUnixEpochTicks: () => number;
}

export type Lumberjack<TPayload extends LumberjackLogPayload | void = void> = ReturnType<
typeof createLumberjack<TPayload>
>;

export function createLumberjack<TPayload extends LumberjackLogPayload | void = void>(
deps: LumberjackDependencies<TPayload>
) {
export function createLumberjack<TPayload extends LumberjackLogPayload | void = void>({
drivers,
logFormatter,
logDriverLogger,
getUnixEpochTicks = () => new Date().valueOf(),
}: LumberjackDependencies<TPayload>) {
const log = (lumberjackLog: LumberjackLog<TPayload>) => {
const { log, formattedLog } = deps.logFormatter.formatLog(lumberjackLog);
logWithErrorHandling(log, formattedLog, deps.drivers);
const { log, formattedLog } = logFormatter.formatLog(lumberjackLog);
logWithErrorHandling(log, formattedLog, drivers);
};

/**
Expand All @@ -51,7 +53,7 @@ export function createLumberjack<TPayload extends LumberjackLogPayload | void =
*/
function createDriverErrorLog(driverError: LumberjackLogDriverError<TPayload>): LumberjackLog<TPayload> {
return {
createdAt: deps.getUnixEpochTicks(),
createdAt: getUnixEpochTicks(),
level: LumberjackLevel.Error,
message: formatLogDriverError(driverError),
scope: 'LumberjackLogDriverError',
Expand Down Expand Up @@ -101,7 +103,7 @@ export function createLumberjack<TPayload extends LumberjackLogPayload | void =
.filter((driver) => canDriveLog(driver, log.level))
.forEach((driver) => {
try {
deps.logDriverLogger.log(driver, { formattedLog, log });
logDriverLogger.log(driver, { formattedLog, log });
stableDrivers.push(driver);

if (driverErrorIndex !== noReportedLogDriverErrorIndex) {
Expand Down
Loading

0 comments on commit 49cf544

Please sign in to comment.