Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update logger.js #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions NodeSample/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,33 @@
* var log = logger("filePath");
* log.writeLog("err",logMessage);
*/
var fs = require('fs');
var buffersize = 30000;
const fs = require('fs');
const buffersize = 30000;

exports.init = function(logfile){
if(logfile){
var buffer = new Buffer(buffersize);
var fd = fs.openSync(logfile,'a');
const buffer = Buffer.alloc(buffersize);
let fd;
try {
fd = await fs.promises.open(logfile, 'a');
} catch (err) {
throw err;
}
}
function writeLog(type,logmsg){
var log = {type:type,msg:logmsg,time:getTime()};
async function writeLog(type,logmsg){
const log = {type:type,msg:logmsg,time:getTime()};
console.log(formatLogMsg(log));
fs.writeSync(fd,formatLogMsg(log),0,0,null);
try {
await fs.promises.write(fd, formatLogMsg(log), 0, 0);
} catch (err) {
throw err;
}
}
return {
log: function(type,logmsg){writeLog(type,logmsg)},
log: async function(type,logmsg){await writeLog(type,logmsg)},
};
}

//格式化日志内容
function formatLogMsg(log){
return [log.time,log.type,log.msg] + "\n";
Expand Down Expand Up @@ -53,4 +63,4 @@ function allPrpos ( obj ) {
}
// 最后显示所有的属性
return props;
}
}