Skip to content

Commit

Permalink
use Date.toLocaleString instead of Date.toLocaleFormat (#94)
Browse files Browse the repository at this point in the history
make SimpleFormatter an ES6 class
fix eslint
  • Loading branch information
lieser committed Dec 17, 2017
1 parent 986efbc commit 7203084
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 44 deletions.
3 changes: 0 additions & 3 deletions globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@

let Sqlite: any;

namespace Log {
declare function exceptionStr(e: Error): string;
}


// The following is for Visual Studio Code IntelliSense only:
Expand Down
63 changes: 22 additions & 41 deletions modules/logging.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ var Logging = {
*
* @param {String} loggerName
*
* @return {Logger} Logger
* @return {Log.Logger} Logger
*/
getLogger: function Logging_getLogger(loggerName){
"use strict";

if (loggerName) {
return Log.repository.getLogger(LOG_NAME + "." + loggerName);
} else {
return Log.repository.getLogger(LOG_NAME);
}
return Log.repository.getLogger(LOG_NAME);
},

/**
Expand All @@ -65,7 +64,7 @@ var Logging = {
* @param {String} loggerName
* @param {String} subPrefBranch
*
* @return {Array} [capp, dapp]
* @return {Array} [consoleAppender, dumpAppender]
*/
addAppenderTo: function Logging_addAppenderTo(loggerName, subPrefBranch){
"use strict";
Expand Down Expand Up @@ -108,49 +107,26 @@ function init() {
/**
* SimpleFormatter
*
* @extends Formatter
* @extends Log.Formatter
*/
function SimpleFormatter(dateFormat) {
"use strict";

if (dateFormat)
this.dateFormat = dateFormat;
}
SimpleFormatter.prototype = Object.create(Log.Formatter.prototype, {
dateFormat: {
get: function() {
"use strict";
if (!this._dateFormat)
this._dateFormat = "%Y-%m-%d %H:%M:%S";
return this._dateFormat;
},
set: function(format) {
"use strict";
this._dateFormat = format;
}
},

format: {
value: function SimpleFormatter_format(message) {
"use strict";
var date = new Date(message.time);
var formatMsg = new String(
date.toLocaleFormat(this.dateFormat) + "\t" +
message.loggerName + "\t" + message.levelDesc + "\t" +
message.message + "\n");
formatMsg.level = message.level;
return formatMsg;
}
class SimpleFormatter extends Log.Formatter {
format(message) {
var date = new Date(message.time);
var formatMsg =
date.toLocaleString(undefined, { hour12: false }) + "\t" +
message.loggerName + "\t" + message.levelDesc + "\t" +
message.message + "\n";
return formatMsg;
}
});
}

// https://wiki.mozilla.org/Labs/JS_Modules#Logging
/**
* setupLogging
*
* @param {String} loggerName
*
* @return {Logger}
* @return {Log.Logger}
*/
function setupLogging(loggerName) {
"use strict";
Expand All @@ -175,16 +151,19 @@ function setupLogging(loggerName) {
/**
* Preference observer for a Logger, ConsoleAppender and DumpAppender
*
* @param {Logger} logger
* @param {ConsoleAppender} capp
* @param {DumpAppender} dapp
* @param {Log.Logger} logger
* @param {Log.ConsoleAppender} capp
* @param {Log.DumpAppender} dapp
* @return {PrefObserver}
*/
function PrefObserver(logger, capp, dapp) {
"use strict";

this.logger = logger;
this.capp = capp;
this.dapp = dapp;

return this;
}
PrefObserver.prototype = {
/*
Expand Down Expand Up @@ -220,6 +199,8 @@ PrefObserver.prototype = {
this.dapp.level = Log.Level[prefs.getCharPref("logging.dump")];
}
break;
default:
// ignore other pref changes
}
},
};
Expand Down
51 changes: 51 additions & 0 deletions mozilla.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ interface nsIPrefBranch {
getBoolPref(aPrefName: string, aDefaultValue?: boolean): boolean;
getCharPref(aPrefName: string, aDefaultValue?: string): string;
getIntPref(aPrefName: string, aDefaultValue?: number): number;
getPrefType(aPrefName: string): number;
prefHasUserValue(aPrefName: string): boolean;
setIntPref(aPrefName: string, aValue: number);
removeObserver(aDomain: string, aObserver: nsIObserver);
readonly PREF_INVALID: number;
readonly PREF_STRING: number;
readonly PREF_INT: number;
readonly PREF_BOOL: number;
}

type nsIObserver = object;
Expand Down Expand Up @@ -90,3 +95,49 @@ interface nsIMsgIdentity {
}

declare function fixIterator<T>(obj: any, type: T): T[]


declare module Log {
function exceptionStr(e: Error): string;

class Appender {
constructor(formatter: Formatter);
level: number;
}
class ConsoleAppender extends Appender {};
class DumpAppender extends Appender {};

class Formatter {}

let repository: LoggerRepository;
let Level: {
All: 0,
Config: 30,
Debug: 20,
// Desc { 0: "ALL", 10: "TRACE", 20: "DEBUG", 30: "CONFIG", 40: "INFO", 50: "WARN", 60: "ERROR", 70: "FATAL" }
Error: 30,
Fatal: 70,
Info: 40,
// Numbers { "ALL": 0, "TRACE": 10, "DEBUG": 20, "CONFIG": 30, "INFO": 40, "WARN": 50, "ERROR": 60, "FATAL": 70 }
Trace: 10,
Warn: 50
}

interface LoggerRepository {
getLogger(name: string): Logger;
}

interface Logger {
addAppender(appender: Appender);

fatal(text: string, params?: Object): void;
error(text: string, params?: Object): void;
warn(text: string, params?: Object): void;
info(text: string, params?: Object): void;
config(text: string, params?: Object): void;
debug(text: string, params?: Object): void;
trace(text: string, params?: Object): void;

level: number;
}
}

0 comments on commit 7203084

Please sign in to comment.