Skip to content

Commit

Permalink
feat(core): add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Jul 2, 2022
1 parent e4ac7d7 commit 51a7984
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
9 changes: 7 additions & 2 deletions packages/classes/mapped-types/src/lib/type-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getMetadataList,
} from '@automapper/classes';
import type { Constructor } from '@automapper/core';
import { AutoMapperLogger } from '@automapper/core';

export function inheritAutoMapMetadata(
parentClass: Constructor,
Expand Down Expand Up @@ -30,7 +31,9 @@ export function inheritAutoMapMetadata(
targetClass
);
} catch (e) {
console.error(`Error trying to inherit metadata: ${e}`);
if (AutoMapperLogger.error) {
AutoMapperLogger.error(`Error trying to inherit metadata: ${e}`);
}
}
}

Expand All @@ -54,6 +57,8 @@ export function inheritPropertyInitializers(
target[propertyName] = tempInstance[propertyName];
});
} catch (e) {
console.error(`Error inheriting properties: ${e}`);
if (AutoMapperLogger.error) {
AutoMapperLogger.error(`Error inheriting properties: ${e}`);
}
}
}
9 changes: 6 additions & 3 deletions packages/classes/src/lib/automap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Constructor } from '@automapper/core';
import { AutoMapperLogger } from '@automapper/core';
import 'reflect-metadata';
import { AUTOMAP_PROPERTIES_METADATA_KEY } from './keys';

Expand Down Expand Up @@ -51,13 +52,15 @@ export function AutoMap(

// if typeFn is still null/undefined, fail fast;
if (options.type == null) {
console.warn(`
if (AutoMapperLogger.warn) {
AutoMapperLogger.warn(`
Cannot determine type metadata of "${String(propertyKey)}" on class ${
target.constructor.name
}.
target.constructor.name
}.
"${String(propertyKey)}" metadata has been skipped.
Manually provide the "type" metadata to prevent unexpected behavior.
`);
}
return;
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './lib/utils/is-empty';
export * from './lib/utils/is-primitive-constructor';
export * from './lib/utils/is-date-constructor';
export * from './lib/utils/set';
export * from './lib/utils/logger';

export * from './lib/mappings/create-map';
export * from './lib/mappings/add-profile';
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
} from './types';
import { Dictionary, MapOptions, ModelIdentifier } from './types';
import { getMapping } from './utils/get-mapping';
import { AutoMapperLogger } from './utils/logger';

export interface CreateMapperOptions {
strategyInitializer: MappingStrategyInitializer<MetadataIdentifier>;
Expand Down Expand Up @@ -173,7 +174,10 @@ Mapper {} is an empty Object as a Proxy. The following methods are available to
if (p === ERROR_HANDLER) {
if (!errorHandler) {
errorHandler = {
handle: console.error.bind(console, '[AutoMapper]'),
handle: AutoMapperLogger.error
? AutoMapperLogger.error.bind(AutoMapperLogger)
: // eslint-disable-next-line @typescript-eslint/no-empty-function
() => {},
};
}
return errorHandler;
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/lib/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const AUTOMAPPER_PREFIX = '[AutoMapper]: ';

export const AutoMapperLogger = {
log: console.log.bind(console, AUTOMAPPER_PREFIX),
warn: console.warn.bind(console, AUTOMAPPER_PREFIX),
error: console.error.bind(console, AUTOMAPPER_PREFIX),
info: console.info.bind(console, AUTOMAPPER_PREFIX),
};

export function configureLogger(logger: Partial<typeof AutoMapperLogger> = {}) {
Object.entries(logger).forEach(([logLevel, logImpl]) => {
if (logImpl !== undefined) {
AutoMapperLogger[logLevel as keyof typeof AutoMapperLogger] =
logImpl;
}
});
}
10 changes: 0 additions & 10 deletions packages/integration-test/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/integration-test/src",
"targets": {
"build": {
"executor": "@nrwl/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/integration-test",
"main": "packages/integration-test/src/index.ts",
"tsConfig": "packages/integration-test/tsconfig.lib.json",
"assets": ["packages/integration-test/*.md"]
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
Expand Down

0 comments on commit 51a7984

Please sign in to comment.