waffle
Version:
シンプルなWEBアプリケーションフレームワークです。(ALL YOUR NODE ARE BELONG TO US)
165 lines (146 loc) • 3.93 kB
JavaScript
/*
* Copyright 2012 Katsunori Koyanagi
*
* 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.
*/
/**
* @overview 内部で使用される簡易ロガーです。
*/
;
/**
* アプリケーション内部で使用される、簡易的なロギング機能を提供します。
* <p>
* 関数として使用した場合、Log.info()と同等に動作します。
* 標準のログライターは、エラーの場合はconsole.error、その他はconsole.logに出力されます。
* </p>
*
* @see Log#info()
* @class アプリケーション内部で使用される、簡易的なロギング機能を提供します。
*/
function Log() {
if (Log.enabled && Log.infoEnabled) {
log0(1, arguments);
}
}
//
// ロガークラスのスタティックメンバ設定
//
/**
* インフォメーションログを出力します。
*
* @function
*/
Log.info = Log;
/**
* エラーログを出力します。
*/
Log.error = function() {
if (Log.enabled && Log.errorEnabled) {
log0(2, arguments);
}
};
/**
* デバッグログを出力します。
*/
Log.debug = function() {
if (log.enabled && Log.debugEnabled) {
log0(0, arguments);
}
};
/**
* インフォメーションログが有効であるかを示します。デフォルトではtrueです。
*
* @type Boolean
*/
Log.infoEnabled = true;
/**
* エラーログが有効であるかを示します。デフォルトではtrueです。
*
* @type Boolean
*/
Log.errorEnabled = true;
/**
* デバッグログが有効であるかを示します。デフォルトではtrueです。
*
* @type Boolean
*/
Log.debugEnabled = true;
/**
* ログ出力機能自体が有効であるかを示します。デフォルトではtrueです。
*
* @type Boolean
*/
Log.enabled = true;
/**
* ログのライターです。
* <p>
* 引数には、ログレベルと、各ログ出力関数に渡された可変長の引数の配列が渡されます。 ログレベルは以下のとおりです。
* </p>
* <table>
* <tr>
* <th>0</th>
* <td>デバッグ</td>
* </tr>
* <tr>
* <th>1</th>
* <td>インフォメーション</td>
* </tr>
* <tr>
* <th>2</th>
* <td>エラー</td>
* </tr>
* </table>
*
* @type Function
*/
Log.writer = defaultWriter;
function defaultWriter(level, args) {
var date = new Date();
var message = "";
var t;
message += date.getFullYear();
message += "-";
message += (t = date.getMonth() + 1) < 10 ? ("0" + t) : t;
message += "-";
message += (t = date.getDate()) < 10 ? ("0" + t) : t;
message += " ";
message += (t = date.getHours()) < 10 ? ("0" + t) : t;
message += ":";
message += (t = date.getMinutes()) < 10 ? ("0" + t) : t;
message += ":";
message += (t = date.getSeconds()) < 10 ? ("0" + t) : t;
message += " (";
message += process.pid;
message += ")";
message += " [";
message += level === 0 ? "DEBUG" : level === 2 ? "ERROR" : "INFO";
message += "] ";
message += args.length > 0 ? args[0] : "";
args[0] = message;
if (level === 2) {
console.error.apply(null, args);
} else {
console.log.apply(null, args);
}
}
function log0(level, args) {
if (Log.writer) {
Log.writer(level, args);
return;
}
defaultWriter(level, args);
}
//
// expose
//
module.exports = Log;