Permalink
Browse files

-

  • Loading branch information...
1 parent 19f5ce1 commit 07c2dbe7a1bf2ad0b35286717c9a6d3a883d62ff @JoeHitchens JoeHitchens committed Sep 9, 2011
Showing with 45 additions and 17 deletions.
  1. +7 −3 README.md
  2. +37 −13 index.js
  3. +1 −1 package.json
View
@@ -1,12 +1,16 @@
# log5 - A simple logging utility.
-This module simply creates global functions log1(), log2(), log3(), log4(), and log5(),
+This module simply creates global functions log1(), log2(), log3(), and log4(),
as well as a global variable called logLevel.
The functions all take a string argument which will be
written to stdout, prefixed with a timestamp, if logLevel is greater than or equal
to the number in the function name.
+You can also install the functions into an object of your choice with inherit(o).
+This will give that object it's own private, independent set of log functions and logLevel.
+
+
## Install
npm install log5
@@ -20,10 +24,10 @@ to the number in the function name.
You are free to make up whatever meanings you want for a given log level.
In practice, log1() messages are typically for things that you probably always want to print
-and log5() would be for more "debug" level messages.
+and log4() would be for more "debug" level messages.
It's simple, it gets the job done, and lets you change the log output at runtime if you like, by just
-setting logLevel.
+changing logLevel.
## License
View
@@ -1,25 +1,49 @@
-global.logLevel = 0
+var num = 4
var n0 = function(n) {
if(n >= 0 && n < 10)
return "0"+n
return n
}
-global.log0 = function(s, l) {
- if(l === undefined)
- l = 0
- if(l <= logLevel) {
- var d = new Date()
- var t = d.getFullYear()+"-"+n0(d.getMonth()+1)+"-"+n0(d.getDate())+"_"+n0(d.getHours())+":"+n0(d.getMinutes())+":"+n0(d.getSeconds())
+var ts = function() {
+ var d = new Date()
+ return d.getFullYear() + "-" +
+ n0(d.getMonth()+1) + "-" +
+ n0(d.getDate()) + "_" +
+ n0(d.getHours()) + ":" +
+ n0(d.getMinutes()) + ":" +
+ n0(d.getSeconds())
+
+}
+
+var mkf = function(l, o) {
+ return function(s) {
+ if(l <= o.logLevel)
+ process.stdout.write(ts()+" "+s+"\n");
+ }
+}
- process.stdout.write(t+" "+s+"\n");
+exports.inherit = function(o, ll) {
+ var i
+ o.logLevel = ll || 0
+ for(i = 0; i <= num; i++) {
+ o["log"+i] = mkf(i, o)
}
}
-global.log1 = function(s) { log0(s, 1) }
-global.log2 = function(s) { log0(s, 2) }
-global.log3 = function(s) { log0(s, 3) }
-global.log4 = function(s) { log0(s, 4) }
-global.log5 = function(s) { log0(s, 5) }
+
+exports.inherit(exports)
+
+// globalize for backward compatibility
+global.logLevel = 0
+global.log0 = exports.log0
+global.log1 = exports.log1
+global.log2 = exports.log2
+global.log3 = exports.log3
+
+if(require.main === module) {
+ require('./test.js')
+}
+
View
@@ -1,6 +1,6 @@
{
"name": "log5",
- "version": "1.0.3",
+ "version": "1.1.0",
"description" : "A simple logging utility",
"author": "Joe Hitchens <joe@sleepless.com> (sleepless.com)",
"license": {

0 comments on commit 07c2dbe

Please sign in to comment.