SHOW:
|
|
- or go back to the newest paste.
1 | //DO NOT PUT "use strict" HERE OR THE LOGGER WILL NOT BE ABLE TO LOG ERROR | |
2 | /* ************************************************************************ | |
3 | ||
4 | - | Version: 0.2 |
4 | + | Version: 0.3 |
5 | ||
6 | - | License: LGPLv3 |
6 | + | License: LGPLv3 |
7 | ||
8 | - | Authors: Deisss |
8 | + | Authors: Deisss |
9 | ||
10 | - | Date: 2012-04-20 |
10 | + | Date: 2012-04-20 |
11 | ||
12 | - | Date of last modification: 2013-04-06 |
12 | + | Date of last modification: 2013-06-07 |
13 | ||
14 | - | Description: Define a shared stdout/stderror shared redirection |
14 | + | Description: Define a shared stdout/stderror shared redirection |
15 | ||
16 | ************************************************************************ */ | |
17 | ||
18 | - | //Do not forget / at then end ! |
18 | + | // Module loaded |
19 | var fs = require("fs"), | |
20 | util = require("util"); | |
21 | - | max_line_number = 10000; |
21 | + | |
22 | // Variables, do not forget / at then end ! | |
23 | - | var fs = require('fs'), |
23 | + | |
24 | - | util = require('util'); |
24 | + | |
25 | ||
26 | - | var consoleLogger = function consoleLogger(){ |
26 | + | max_line_number = 10000, |
27 | - | //defining a var instead of this (works for variable & function) will create a private definition |
27 | + | stdout = null, |
28 | - | var stdout = null, |
28 | + | stderr = null, |
29 | - | stderr = null, |
29 | + | stdoutPath = "", |
30 | - | stdoutPath = "", |
30 | + | stderrPath = "", |
31 | - | stderrPath = "", |
31 | + | maxct = max_line_number; |
32 | - | maxct = max_line_number; |
32 | + | |
33 | /* | |
34 | - | // Get the main OS path, create it if needed |
34 | + | -------------------------------------- |
35 | - | var getOSRootPath = function() { |
35 | + | TIME FUNCTION |
36 | - | var path = ""; |
36 | + | -------------------------------------- |
37 | - | if(process.platform === 'win32') { |
37 | + | |
38 | - | path = windows_error_log_root; |
38 | + | |
39 | - | } else { |
39 | + | * Print a date with "0" before if it's less than 10 |
40 | - | path = unix_error_log_root; |
40 | + | * |
41 | * @param variable {Integer} A number (from date) | |
42 | * @return {String} The parsed date | |
43 | - | //Test or create the directory, we do it synchronously to never loose any log message |
43 | + | |
44 | - | try{ |
44 | + | function beautifulDate(variable) { |
45 | - | if(!fs.statSync(windows_error_log_root).isDirectory()){ |
45 | + | var prefix = (variable < 10) ? "0" : ""; |
46 | - | fs.mkdirSync(windows_error_log_root, 0600); |
46 | + | return prefix + variable; |
47 | - | } |
47 | + | }; |
48 | - | }catch(e){ |
48 | + | |
49 | /** | |
50 | * Print in a nice way the output date | |
51 | * | |
52 | - | return path; |
52 | + | * @return {String} The formatted date |
53 | */ | |
54 | function outputDate() { | |
55 | - | // Create the main stream |
55 | + | var d = new Date(); |
56 | - | var createStream = function() { |
56 | + | return ("" + d.getFullYear() + "-" + beautifulDate(d.getMonth()+1) + "-" + beautifulDate(d.getDate()) + " " + |
57 | - | var path = getOSRootPath(); |
57 | + | beautifulDate(d.getHours()) + "." + beautifulDate(d.getMinutes()) + "." + beautifulDate(d.getSeconds())); |
58 | }; | |
59 | - | stdoutPath = path + "access.log"; |
59 | + | |
60 | - | stderrPath = path + "error.log"; |
60 | + | |
61 | - | stdout = fs.createWriteStream(stdoutPath, { flags: 'a' }); |
61 | + | * Return a formatted date string |
62 | - | stderr = fs.createWriteStream(stderrPath, { flags: 'a' }); |
62 | + | * |
63 | * @return {String} The formatted date | |
64 | */ | |
65 | - | // Close the main stream |
65 | + | var printDate = function() { |
66 | - | var closeStream = function() { |
66 | + | var d = new Date(); |
67 | - | stdout.end(); |
67 | + | var tmp = outputDate(); |
68 | - | stderr.end(); |
68 | + | return (tmp.replace(".", ":") + " (UTC : " + (d.getTimezoneOffset()/60) + "h) => "); |
69 | }; | |
70 | ||
71 | - | // Close streams and re-open them after few lines |
71 | + | /* |
72 | - | var checkAndCloseStream = function() { |
72 | + | -------------------------------------- |
73 | - | maxct--; |
73 | + | PATH FUNCTION |
74 | - | if(maxct <= 0) { |
74 | + | -------------------------------------- |
75 | - | closeStream(); |
75 | + | |
76 | /** | |
77 | - | //Now we move log files to something usable |
77 | + | * Get the main OS path, create it if needed |
78 | - | var d = outputDate(); |
78 | + | |
79 | - | fs.renameSync(stdoutPath, getOSRootPath() + "access-" + d + ".log"); |
79 | + | function getOSRootPath() { |
80 | - | fs.renameSync(stderrPath, getOSRootPath() + "error-" + d + ".log"); |
80 | + | var path = ""; |
81 | if(process.platform === "win32") { | |
82 | - | //Now we create again stream |
82 | + | path = windows_error_log_root; |
83 | - | createStream(); |
83 | + | } else { |
84 | - | maxct = max_line_number; |
84 | + | path = unix_error_log_root; |
85 | } | |
86 | ||
87 | //Test or create the directory, we do it synchronously to never loose any log message | |
88 | - | //Now we log the system |
88 | + | try { |
89 | - | process.__defineGetter__("stdout", function(){ |
89 | + | if(!fs.statSync(windows_error_log_root).isDirectory()) { |
90 | - | return stdout; |
90 | + | |
91 | - | }); |
91 | + | |
92 | - | process.__defineGetter__("stderr", function(){ |
92 | + | } catch(e) { |
93 | - | return stderr; |
93 | + | fs.mkdirSync(windows_error_log_root, 0600); |
94 | - | }); |
94 | + | |
95 | ||
96 | return path; | |
97 | - | process.on("uncaughtException", function(err){ |
97 | + | }; |
98 | - | this.error("Uncaught exception : " + err); |
98 | + | |
99 | /* | |
100 | -------------------------------------- | |
101 | - | //Return a beautiful date printed, add 0 if variable is less to 10 |
101 | + | STREAM FUNCTION |
102 | - | var beautifulDate = function(variable){ |
102 | + | -------------------------------------- |
103 | - | if(variable < 10){ |
103 | + | |
104 | - | return "0" + variable; |
104 | + | |
105 | * Start log stream system | |
106 | - | return variable; |
106 | + | |
107 | function createStream() { | |
108 | var path = getOSRootPath(); | |
109 | - | // Used for log storage |
109 | + | |
110 | - | var outputDate = function() { |
110 | + | stdoutPath = path + "access.log"; |
111 | - | var d = new Date(); |
111 | + | stderrPath = path + "error.log"; |
112 | - | return ("" + d.getFullYear() + "-" + beautifulDate(d.getMonth()+1) + "-" + beautifulDate(d.getDate()) + " " + beautifulDate(d.getHours()) + |
112 | + | stdout = fs.createWriteStream(stdoutPath, { flags: "a" }); |
113 | - | "." + beautifulDate(d.getMinutes()) + "." + beautifulDate(d.getSeconds())); |
113 | + | stderr = fs.createWriteStream(stderrPath, { flags: "a" }); |
114 | }; | |
115 | ||
116 | - | //Return a formated date string |
116 | + | |
117 | - | var printDate = function(){ |
117 | + | * Close log stream system |
118 | - | var d = new Date(); |
118 | + | |
119 | - | var tmp = outputDate(); |
119 | + | function closeStream() { |
120 | - | return (tmp.replace(".", ":") + " (UTC : " + (d.getTimezoneOffset()/60) + "h) => "); |
120 | + | stdout.end(); |
121 | stderr.end(); | |
122 | }; | |
123 | - | //This function replace the usual console.log, it just automatically add date |
123 | + | |
124 | - | this.log = function(){ |
124 | + | |
125 | - | if(stdout != null && stdout.writable===true){ |
125 | + | * Close stream if needed |
126 | */ | |
127 | function checkAndCloseStream() { | |
128 | maxct--; | |
129 | if(maxct <= 0) { | |
130 | closeStream(); | |
131 | - | //This function replace the usual console.dir, it just automatically add date |
131 | + | |
132 | - | this.dir = function(object){ |
132 | + | //Now we move log files to something usable |
133 | - | if(stdout != null && stdout.writable===true){ |
133 | + | var d = outputDate(); |
134 | fs.renameSync(stdoutPath, getOSRootPath() + "access-" + d + ".log"); | |
135 | fs.renameSync(stderrPath, getOSRootPath() + "error-" + d + ".log"); | |
136 | ||
137 | //Now we create again stream | |
138 | createStream(); | |
139 | - | //This function replace usual console.error, it just automatically add date |
139 | + | maxct = max_line_number; |
140 | - | this.error = function(){ |
140 | + | |
141 | - | if(stderr != null && stderr.writable===true){ |
141 | + | }; |
142 | ||
143 | /** | |
144 | * Console logger object | |
145 | */ | |
146 | module.exports = new function() { | |
147 | /** | |
148 | * Print a log (any number of arguments allowed) | |
149 | - | //Remove instanciate possibility for other module |
149 | + | */ |
150 | - | if(consoleLogger.caller != consoleLogger.getInstance){ |
150 | + | this.log = function() { |
151 | - | throw new Error("This object cannot be instanciated"); |
151 | + | console.log.apply(this, arguments); |
152 | if(stdout != null && stdout.writable===true) { | |
153 | stdout.write(printDate() + util.format.apply(this, arguments) + "\n"); | |
154 | checkAndCloseStream(); | |
155 | } | |
156 | - | } |
156 | + | |
157 | ||
158 | /** | |
159 | - | CONSOLE LOGGER SINGLETON CLASS DEFINITION |
159 | + | * Print dir (any number of arguments allowed) |
160 | */ | |
161 | - | consoleLogger.instance = null; |
161 | + | this.dir = function(object) { |
162 | console.dir.apply(this, arguments); | |
163 | if(stdout != null && stdout.writable===true) { | |
164 | - | * consoleLogger getInstance definition |
164 | + | |
165 | - | * @return consoleLogger class |
165 | + | |
166 | } | |
167 | - | consoleLogger.getInstance = function(){ |
167 | + | |
168 | - | if(this.instance === null){ |
168 | + | |
169 | - | this.instance = new consoleLogger(); |
169 | + | /** |
170 | * Print an error (any number of arguments allowed) | |
171 | - | return this.instance; |
171 | + | */ |
172 | - | } |
172 | + | this.error = function() { |
173 | console.error.apply(this, arguments); | |
174 | - | module.exports = consoleLogger.getInstance(); |
174 | + | if(stderr != null && stderr.writable===true) { |
175 | stderr.write(printDate() + util.format.apply(this, arguments) + "\n"); | |
176 | checkAndCloseStream(); | |
177 | } | |
178 | }; | |
179 | // Alias | |
180 | this.err = this.error; | |
181 | ||
182 | //Catching all system error | |
183 | process.on("uncaughtException", function(err) { | |
184 | this.error("Uncaught exception : "); | |
185 | this.error(err); | |
186 | }.bind(this)); | |
187 | ||
188 | //We create the stream | |
189 | createStream(); | |
190 | }; |