MOBILE-2261 core: Implement Logger service

main
Dani Palou 2017-10-23 10:58:03 +02:00
parent 100996870f
commit d6b2523daf
5 changed files with 86 additions and 6 deletions

View File

@ -1,7 +1,7 @@
Moodle Mobile 2.x Moodle Mobile
================= =================
This is the primary repository of source code for the official Moodle Mobile app version 2.x. This is the primary repository of source code for the official Moodle Mobile app.
* [User documentation](http://docs.moodle.org/en/Moodle_Mobile) * [User documentation](http://docs.moodle.org/en/Moodle_Mobile)
* [Developer documentation](http://docs.moodle.org/dev/Moodle_Mobile) * [Developer documentation](http://docs.moodle.org/dev/Moodle_Mobile)

5
package-lock.json generated
View File

@ -2752,6 +2752,11 @@
} }
} }
}, },
"moment": {
"version": "2.19.1",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.19.1.tgz",
"integrity": "sha1-VtoaLRy/AdOLfhr8McELz6GSkWc="
},
"ms": { "ms": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",

View File

@ -38,13 +38,14 @@
"@ionic-native/sqlite": "^4.3.2", "@ionic-native/sqlite": "^4.3.2",
"@ionic-native/status-bar": "4.3.0", "@ionic-native/status-bar": "4.3.0",
"@ionic/storage": "2.0.1", "@ionic/storage": "2.0.1",
"electron-builder-squirrel-windows": "^19.3.0",
"electron-windows-notifications": "^1.1.13",
"ionic-angular": "3.7.1", "ionic-angular": "3.7.1",
"ionicons": "3.0.0", "ionicons": "3.0.0",
"moment": "^2.19.1",
"rxjs": "5.4.3", "rxjs": "5.4.3",
"sw-toolbox": "3.6.0", "sw-toolbox": "3.6.0",
"zone.js": "0.8.18", "zone.js": "0.8.18"
"electron-windows-notifications": "^1.1.13",
"electron-builder-squirrel-windows": "^19.3.0"
}, },
"devDependencies": { "devDependencies": {
"@ionic/app-scripts": "3.0.0", "@ionic/app-scripts": "3.0.0",

View File

@ -5,6 +5,7 @@ import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar'; import { StatusBar } from '@ionic-native/status-bar';
import { MyApp } from './app.component'; import { MyApp } from './app.component';
import { CoreLoggerProvider } from '../providers/logger';
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -21,7 +22,8 @@ import { MyApp } from './app.component';
providers: [ providers: [
StatusBar, StatusBar,
SplashScreen, SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler} {provide: ErrorHandler, useClass: IonicErrorHandler},
CoreLoggerProvider
] ]
}) })
export class AppModule {} export class AppModule {}

View File

@ -0,0 +1,72 @@
// (C) Copyright 2015 Martin Dougiamas
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import { Injectable } from '@angular/core';
import * as moment from 'moment';
/**
* Helper service to display messages in the console.
*
* @description
* This service is meant to improve log messages, adding a timestamp and a name to all log messages.
*
* In your class constructor, call getInstance to configure your class name:
* this.logger = logger.getInstance('InitPage');
*
* Then you can call the log function you want to use in this logger instance.
*/
@Injectable()
export class CoreLoggerProvider {
/** Whether the logging is enabled. */
public enabled: boolean = true;
constructor() {}
/**
* Get a logger instance for a certain class, service or component.
*
* @param {string} className Name to use in the messages.
* @return {ant} Instance.
*/
getInstance(className: string) : any {
className = className || '';
return {
log : this.prepareLogFn(console.log.bind(console), className),
info : this.prepareLogFn(console.info.bind(console), className),
warn : this.prepareLogFn(console.warn.bind(console), className),
debug : this.prepareLogFn(console.debug.bind(console), className),
error : this.prepareLogFn(console.error.bind(console), className)
};
}
/**
* Prepare a logging function, concatenating the timestamp and class name to all messages.
*
* @param {Function} logFn Log function to use.
* @param {string} className Name to use in the messages.
* @return {Function} Prepared function.
*/
private prepareLogFn(logFn: Function, className: string) : Function {
// Return our own function that will call the logging function with the treated message.
return (...args) => {
if (this.enabled) {
let now = moment().format('l LTS');
args[0] = now + ' ' + className + ': ' + args[0]; // Prepend timestamp and className to the original message.
logFn.apply(null, args);
}
};
}
}