Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#17 from openSUSE/logger-service
Browse files Browse the repository at this point in the history
Add shared logger service
  • Loading branch information
jeefy authored May 11, 2019
2 parents 433c149 + 98bf500 commit 5c8251e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { OptionsComponent } from './options/options.component';
import { FormsModule } from '@angular/forms';
import { MarkdownPipe } from './markdown-pipe';
import { NgxPaginationModule } from 'ngx-pagination';
import { LoggerService } from '@shared/services/logger.service';

import { EffectsModule } from '@ngrx/effects';
import { NotesEffects } from './notes/notes.effects';
Expand All @@ -31,7 +32,9 @@ import { reducers } from './app.reducer';
EffectsModule.forRoot([NotesEffects]),
StoreDevtoolsModule.instrument({ maxAge: 25, logOnly: environment.production }),
],
providers: [],
bootstrap: [AppComponent],
providers: [
LoggerService
],
bootstrap: [AppComponent]
})
export class AppModule {}
9 changes: 6 additions & 3 deletions src/app/notes/notes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';

import { Note } from './notes.model';
import { LoggerService } from '@shared/services/logger.service';

@Injectable({
providedIn: 'root',
})
export class NotesService {
noteUrl = './assets/data/release-notes.json';

constructor(private http: HttpClient) {}
constructor(
private http: HttpClient,
private logger: LoggerService,
) {}

getNotes(filter): Observable<Note[]> {
console.log('Gathering notes');

this.logger.debug('Gathering notes');
return this.http.get<Note[]>(this.noteUrl).pipe();
}
}
60 changes: 60 additions & 0 deletions src/app/shared/services/logger.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Injectable } from '@angular/core';
import { environment } from '@environment';

/**
* The logger service implementation
*/
@Injectable()
export class LoggerService {
/**
* Gets the current time as prefix
*
* @returns The time prefix.
*/
private date(): string {
const date = new Date();
return date.toLocaleString();
}

/**
* Logs a debug message to the console when log level >= 3
*
* @param msg The log message
*/
public debug(msg: string) {
if (environment.logLevel >= 3) {
console.log(`[DEBUG]\t${this.date()}: ${msg}`);
}
}

/**
* Logs an info message to the console when log level >= 2
*
* @param msg The info log message
*/
public info(msg: string) {
if (environment.logLevel >= 2) {
console.log(`[INFO]\t${this.date()}: ${msg}`);
}
}

/**
* Logs a warning to the console when log level >= 1
*
* @param msg The warning log message
*/
public warn(msg: string) {
if (environment.logLevel >= 1) {
console.log(`[WARN]\t${this.date()}: ${msg}`);
}
}

/**
* Always logs an error message to the console
*
* @param msg The error log message
*/
public error(msg: string) {
console.log(`[ERROR]\t${this.date()}: ${msg}`);
}
}
1 change: 1 addition & 0 deletions src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const environment = {
production: true,
logLevel: 0 // 0 - 3 verbosity
};
1 change: 1 addition & 0 deletions src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

export const environment = {
production: false,
logLevel: 3 // 0 - 3 verbosity
};

/*
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"outDir": "./dist/out-tsc",
"paths": {
"@app/*": ["./src/app/*"],
"@environment": ["./src/environments/environment"]
"@environment": ["./src/environments/environment"],
"@shared/*": ["src/app/shared/*"]
},
"sourceMap": true,
"declaration": false,
Expand Down

0 comments on commit 5c8251e

Please sign in to comment.