egg-logrotater
Advanced tools
Comparing version 0.0.4 to 1.0.0
@@ -7,9 +7,7 @@ 'use strict'; | ||
module.exports = function(app) { | ||
module.exports = app => { | ||
const exports = {}; | ||
const logger = app.coreLogger; | ||
const loggerConfig = app.config.logger; | ||
const rotateLogDirs = loggerConfig.rotateLogDirs; | ||
const rotateLogDirs = app.config.logger.rotateLogDirs; | ||
const messenger = app.messenger; | ||
@@ -21,18 +19,25 @@ | ||
}; | ||
// 开始切割指定目录下所有的 log 文件 | ||
exports.task = function* () { | ||
const maxDays = app.config.logrotater.maxDays; | ||
if (maxDays && maxDays > 0) { | ||
try { | ||
yield rotateLogDirs.map(logdir => removeExpiredLogFiles(logdir, maxDays)); | ||
} catch (err) { | ||
logger.error(err); | ||
} | ||
} | ||
try { | ||
const tasks = rotateLogDirs.map(logdir => renameLogfiles(logdir)); | ||
yield tasks; | ||
// tell master send reload logger message | ||
logger.info('[egg-logrotater] broadcast log-reload to workers'); | ||
messenger.sendToApp('log-reload'); | ||
messenger.sendToAgent('log-reload'); | ||
yield rotateLogDirs.map(logdir => renameLogfiles(logdir)); | ||
} catch (err) { | ||
logger.error(err); | ||
} | ||
// tell every one to reload logger | ||
logger.info('[egg-logrotater] broadcast log-reload to workers'); | ||
messenger.sendToApp('log-reload'); | ||
messenger.sendToAgent('log-reload'); | ||
}; | ||
// 将目录下的日志文件全部重命名 | ||
// rename xxx.log => xxx.log.YYYY-MM-DD | ||
function* renameLogfiles(logdir) { | ||
@@ -64,3 +69,35 @@ const logname = moment().subtract(1, 'days').format('.YYYY-MM-DD'); | ||
// remove expired log files: xxx.log.YYYY-MM-DD | ||
function* removeExpiredLogFiles(logdir, maxDays) { | ||
const files = yield fs.readdir(logdir); | ||
const expriedDate = moment().subtract(maxDays, 'days').startOf('date'); | ||
const names = files.filter(file => { | ||
const name = path.extname(file).substring(1); | ||
if (!/^\d{4}\-\d{2}\-\d{2}$/.test(name)) { | ||
return false; | ||
} | ||
const date = moment(name, 'YYYY-MM-DD').startOf('date'); | ||
if (!date.isValid()) { | ||
return false; | ||
} | ||
return date.isBefore(expriedDate); | ||
}); | ||
if (names.length === 0) { | ||
return; | ||
} | ||
logger.info(`[egg-logrotater] start remove ${logdir} files: ${names.join(', ')}`); | ||
yield names.map(name => function* () { | ||
const logfile = path.join(logdir, name); | ||
try { | ||
yield fs.unlink(logfile); | ||
} catch (err) { | ||
err.message = `[egg-logrotater] remove logfile ${logfile} error, ${err.message}`; | ||
logger.error(err); | ||
} | ||
}); | ||
} | ||
return exports; | ||
}; |
@@ -5,4 +5,3 @@ 'use strict'; | ||
module.exports = function(app) { | ||
module.exports = app => { | ||
const exports = {}; | ||
@@ -20,7 +19,6 @@ | ||
type: 'worker', | ||
disable: filesRotateBySize.length === 0, // 没配置则关闭 | ||
disable: filesRotateBySize.length === 0, | ||
}; | ||
exports.task = function* checkFileSize() { | ||
let needSendMessage = false; | ||
@@ -45,4 +43,2 @@ | ||
} | ||
}; | ||
@@ -49,0 +45,0 @@ |
@@ -6,6 +6,7 @@ 'use strict'; | ||
* @member Config#logrotater | ||
* @property {Array} filesRotateBySize - 需要按大小切割的文件,其他日志文件仍按照通常方式切割 | ||
* @property {Number} maxFileSize - 最大文件大小,默认为50m | ||
* @property {Number} maxFiles - 按大小切割时,文件最大切割的份数 | ||
* @property {Number} rotateDuration - 按大小切割时,文件扫描的间隔时间 | ||
* @property {Array} filesRotateBySize - Array for files path which need rotate | ||
* @property {Number} maxFileSize - Max file size to judge if any file need rotate | ||
* @property {Number} maxFiles - pieces rotate by size | ||
* @property {Number} maxDays - keep max days log files, default is `31`. Set `0` to keep all logs. | ||
* @property {Number} rotateDuration - time interval to judge if any file need rotate | ||
*/ | ||
@@ -17,2 +18,3 @@ exports.logrotater = { | ||
rotateDuration: 60000, | ||
maxDays: 31, | ||
}; |
{ | ||
"name": "egg-logrotater", | ||
"version": "0.0.4", | ||
"version": "1.0.0", | ||
"description": "logrotater for egg", | ||
@@ -27,13 +27,13 @@ "eggPlugin": { | ||
"devDependencies": { | ||
"autod": "2", | ||
"egg": "*", | ||
"egg-bin": "1", | ||
"egg-ci": "1", | ||
"egg-bin": "1", | ||
"egg": "0.0.5", | ||
"egg-mock": "0.0.2", | ||
"autod": "2", | ||
"egg-mock": "*", | ||
"eslint": "3", | ||
"eslint-config-egg": "3", | ||
"glob": "6", | ||
"pedding": "1", | ||
"should": "8", | ||
"supertest": "1", | ||
"glob": "6", | ||
"pedding": "1" | ||
"supertest": "1" | ||
}, | ||
@@ -47,3 +47,3 @@ "engines": { | ||
"cov": "egg-bin cov", | ||
"lint": "eslint --ext js . --fix", | ||
"lint": "eslint --ext js --fix test app config *.js", | ||
"ci": "npm run lint && npm run cov", | ||
@@ -50,0 +50,0 @@ "autod": "autod" |
@@ -40,3 +40,3 @@ # egg-logrotater | ||
- `config.js` | ||
- `config.default.js` | ||
@@ -47,5 +47,6 @@ ```js | ||
filesRotateBySize: [], // Array for files path which need rotate. | ||
maxFileSize: 60 * 1024 * 1024, // Max file size to judge if any file need rotate | ||
maxFileSize: 50 * 1024 * 1024, // Max file size to judge if any file need rotate | ||
maxFiles: 10, // pieces rotate by size | ||
rotateDuration: 60000, // time interval to judge if any file need rotate | ||
maxDays: 31, // keep max days log files, default is `31`. Set `0` to keep all logs | ||
}; | ||
@@ -52,0 +53,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10560
7
173
0
60