Skip to content

Commit

Permalink
refactor: improve logging (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
subotic committed Jun 7, 2023
1 parent e5046d4 commit da08b54
Show file tree
Hide file tree
Showing 22 changed files with 115 additions and 69 deletions.
6 changes: 3 additions & 3 deletions apps/dateAdapter/src/main.ts
Expand Up @@ -5,9 +5,9 @@ import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
enableProdMode();
enableProdMode();
}

platformBrowserDynamic()
.bootstrapModule(AppModule)
.catch((err) => console.log(err));
.bootstrapModule(AppModule)
.catch((err) => console.error(err));
6 changes: 3 additions & 3 deletions apps/dsp-app/src/app/app.module.ts
Expand Up @@ -171,7 +171,7 @@ import { ProjectTileComponent } from './system/project-tile/project-tile.compone
import { CommentFormComponent } from './workspace/resource/values/comment-form/comment-form.component';
import { DataModelsComponent } from './project/data-models/data-models.component';
import { ResourceClassPropertyInfoComponent } from '@dsp-app/src/app/project/ontology/resource-class-info/resource-class-property-info/resource-class-property-info.component';
import { AppLogsService } from '@dasch-swiss/vre/shared/app-logs';
import { AppLoggingService } from '@dasch-swiss/vre/shared/app-logging';

// translate: AoT requires an exported function for factories
export function httpLoaderFactory(httpClient: HttpClient) {
Expand Down Expand Up @@ -353,7 +353,7 @@ export function httpLoaderFactory(httpClient: HttpClient) {
providers: [
AppConfigService,
DatadogRumService,
AppLogsService,
AppLoggingService,
{
provide: DspApiConfigToken,
useFactory: (appConfigService: AppConfigService) =>
Expand Down Expand Up @@ -381,7 +381,7 @@ export function httpLoaderFactory(httpClient: HttpClient) {
{
provide: ErrorHandler,
useClass: AppErrorHandler,
deps: [AppLogsService],
deps: [AppLoggingService],
},
],
bootstrap: [AppComponent],
Expand Down
Expand Up @@ -174,7 +174,7 @@ export class AddValueComponent implements OnInit, AfterViewInit {
);
break;
default:
console.log(
console.error(
'There was an error processing your request. Details: ',
error
);
Expand Down
Expand Up @@ -343,7 +343,7 @@ export class DisplayEditComponent implements OnInit {
);
break;
default:
console.log(
console.error(
'There was an error processing your request. Details: ',
error
);
Expand Down
Expand Up @@ -32,8 +32,6 @@ export class ReplaceFileFormComponent implements OnInit {
fileValue: UpdateFileValue;
warningMessages: string[];

constructor() {}

ngOnInit(): void {
this._generateWarningMessage(this.representation);
}
Expand All @@ -50,7 +48,7 @@ export class ReplaceFileFormComponent implements OnInit {
updateVal.id = this.propId;
this.closeDialog.emit(updateVal);
} else {
console.log('expected UpdateFileValue, got: ', updateVal);
console.error('expected UpdateFileValue, got: ', updateVal);
}
}

Expand Down
Expand Up @@ -102,7 +102,7 @@ export class SearchListValueComponent
const listNodeIri = guiAttr[0].substring(7, guiAttr[0].length - 1); // hlist=<>, get also rid of <>
return listNodeIri;
} else {
console.log('No root node Iri given for property');
console.error('No root node Iri given for property', guiAttr);
}
}
}
Expand Up @@ -176,18 +176,18 @@ export class SpecifyPropertyValueComponent implements OnChanges, OnDestroy {
case Constants.Resource: // tODO: Match is only available on top level
this.comparisonOperators = this.topLevel
? [
new Equals(),
new NotEquals(),
new Exists(),
new NotExists(),
new Match(),
]
new Equals(),
new NotEquals(),
new Exists(),
new NotExists(),
new Match(),
]
: [
new Equals(),
new NotEquals(),
new Exists(),
new NotExists(),
];
new Equals(),
new NotEquals(),
new Exists(),
new NotExists(),
];
break;

case Constants.IntValue:
Expand Down Expand Up @@ -225,10 +225,7 @@ export class SpecifyPropertyValueComponent implements OnChanges, OnDestroy {
case Constants.IntervalValue:
case Constants.GeonameValue:
case Constants.TimeValue:
this.comparisonOperators = [
new Exists(),
new NotExists(),
];
this.comparisonOperators = [new Exists(), new NotExists()];
break;

default:
Expand All @@ -249,8 +246,10 @@ export class SpecifyPropertyValueComponent implements OnChanges, OnDestroy {
let value: Value;

// comparison operator 'Exists' does not require a value
if (this.comparisonOperatorSelected.getClassName() !== 'Exists' &&
this.comparisonOperatorSelected.getClassName() !== 'NotExists') {
if (
this.comparisonOperatorSelected.getClassName() !== 'Exists' &&
this.comparisonOperatorSelected.getClassName() !== 'NotExists'
) {
value = this.propertyValueComponent.getValue();
}

Expand Down
@@ -1,13 +1,29 @@
import { ErrorHandler } from '@angular/core';
import { ErrorHandler, Injector } from '@angular/core';
import { AppLoggingService } from '@dasch-swiss/vre/shared/app-logging';
import { HttpErrorResponse } from '@angular/common/http';

export class AppErrorHandler implements ErrorHandler {
constructor(private _injector: Injector) {}

/**
* Logs out the error to the console.
* The AppLogsService is configured in such a way to catch errors printed
* to the console and forward them to DataDog.
* @param err the error to log.
* Logs out the error using the logging service.
* @param error the error to log.
*/
handleError(err: any): void {
console.error(err.originalError || err);
handleError(error: Error): void {
const logger = this._injector.get(AppLoggingService);

if (error instanceof HttpErrorResponse) {
// HTTP related error
logger.error('Caught HttpErrorResponse error', error);
} else if (
error instanceof TypeError ||
error instanceof ReferenceError
) {
// Runtime exceptions mostly induced by Developer's code
logger.error('Caught Type or Reference error', error);
} else {
// catch-all: catch rest of errors
logger.error('Caught error', error);
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions libs/vre/shared/app-logging/src/index.ts
@@ -0,0 +1 @@
export * from './lib/app-logging/app-logging.service';
@@ -1,13 +1,13 @@
import { TestBed } from '@angular/core/testing';

import { AppLogsService } from './app-logs.service';
import { AppLoggingService } from './app-logging.service';

describe('AppLogsService', () => {
let service: AppLogsService;
describe('AppLoggingService', () => {
let service: AppLoggingService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(AppLogsService);
service = TestBed.inject(AppLoggingService);
});

it('should be created', () => {
Expand Down
@@ -0,0 +1,59 @@
import { Inject, Injectable } from '@angular/core';
import { datadogLogs, Logger } from '@datadog/browser-logs';
import {
DspInstrumentationConfig,
DspInstrumentationToken,
} from '@dasch-swiss/vre/shared/app-config';

@Injectable({
providedIn: 'root',
})
export class AppLoggingService {
private logger: Logger | undefined;
constructor(
@Inject(DspInstrumentationToken) private c: DspInstrumentationConfig
) {
if (c.dataDog.enabled && typeof c.dataDog.clientToken == 'string') {
datadogLogs.init({
clientToken: c.dataDog.clientToken,
site: c.dataDog.site,
service: c.dataDog.service,
env: c.environment,
forwardErrorsToLogs: true, // forwards console.error logs, uncaught exceptions and network errors to Datadog
forwardConsoleLogs: [], // don't forward any logs (besides console.error - in previous setting) to Datadog
});
datadogLogs.logger.setHandler(['console', 'http']);
this.logger = datadogLogs.logger;
}
}

debug(message: string, messageContext?: object, error?: Error) {
if (typeof this.logger != 'undefined') {
datadogLogs.logger.debug(message, messageContext, error);
} else {
console.debug(message, messageContext, error);
}
}

info(message: string, messageContext?: object, error?: Error) {
if (typeof this.logger != 'undefined') {
this.logger.info(message, messageContext, error);
} else {
console.info(message, messageContext, error);
}
}
warn(message: string, messageContext?: object, error?: Error) {
if (typeof this.logger != 'undefined') {
this.logger.warn(message, messageContext, error);
} else {
console.warn(message, messageContext, error);
}
}
error(message: string, messageContext?: any, error?: Error) {
if (typeof this.logger != 'undefined') {
this.logger.error(message, messageContext, error);
} else {
console.error(message, messageContext, error);
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion libs/vre/shared/app-logs/src/index.ts

This file was deleted.

26 changes: 0 additions & 26 deletions libs/vre/shared/app-logs/src/lib/app-logs/app-logs.service.ts

This file was deleted.

4 changes: 2 additions & 2 deletions tsconfig.base.json
Expand Up @@ -30,8 +30,8 @@
"@dasch-swiss/vre/shared/app-error-handler": [
"libs/vre/shared/app-error-handler/src/index.ts"
],
"@dasch-swiss/vre/shared/app-logs": [
"libs/vre/shared/app-logs/src/index.ts"
"@dasch-swiss/vre/shared/app-logging": [
"libs/vre/shared/app-logging/src/index.ts"
],
"@dsp-app/*": ["apps/dsp-app/*"]
}
Expand Down

0 comments on commit da08b54

Please sign in to comment.