Skip to content

Commit

Permalink
feat: rename logrotater => logrotator (#4)
Browse files Browse the repository at this point in the history
* feat: rename logrotater => logrotator

* test: more testcase
  • Loading branch information
popomore authored Aug 17, 2016
1 parent 7793b2f commit 0fe84da
Show file tree
Hide file tree
Showing 19 changed files with 121 additions and 98 deletions.
21 changes: 0 additions & 21 deletions .github/ISSUE_TEMPLATE.md

This file was deleted.

33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# egg-logrotater
# egg-logrotator

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
Expand All @@ -7,25 +7,25 @@
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-logrotater.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-logrotater
[travis-image]: https://img.shields.io/travis/eggjs/egg-logrotater.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-logrotater
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-logrotater.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-logrotater?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-logrotater.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-logrotater
[snyk-image]: https://snyk.io/test/npm/egg-logrotater/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-logrotater
[download-image]: https://img.shields.io/npm/dm/egg-logrotater.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-logrotater
[npm-image]: https://img.shields.io/npm/v/egg-logrotator.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-logrotator
[travis-image]: https://img.shields.io/travis/eggjs/egg-logrotator.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-logrotator
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-logrotator.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-logrotator?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-logrotator.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-logrotator
[snyk-image]: https://snyk.io/test/npm/egg-logrotator/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-logrotator
[download-image]: https://img.shields.io/npm/dm/egg-logrotator.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-logrotator

Log rotate plugin for egg, default rotate log files under `config.logger.rotateLogDirs`.Run by [egg-schedule](https://github.com/eggjs/egg-schedule)

## Install

```bash
$ npm i egg-logrotater
$ npm i egg-logrotator
```

## Usage
Expand All @@ -34,14 +34,14 @@ $ npm i egg-logrotater
- `plugin.js`

```js
exports.logrotater = true;
exports.logrotator = true;
```

- `config.default.js`

```js
// if any files need rotate by file size, config here
exports.logrotater = {
exports.logrotator = {
filesRotateBySize: [], // Array for files path which need rotate.
maxFileSize: 50 * 1024 * 1024, // Max file size to judge if any file need rotate
maxFiles: 10, // pieces rotate by size
Expand All @@ -57,3 +57,4 @@ Please open an issue [here](https://github.com/eggjs/egg/issues).
## License

[MIT](LICENSE)

30 changes: 15 additions & 15 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# egg-logrotater
# egg-logrotator

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
Expand All @@ -7,18 +7,18 @@
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-logrotater.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-logrotater
[travis-image]: https://img.shields.io/travis/eggjs/egg-logrotater.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-logrotater
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-logrotater.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-logrotater?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-logrotater.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-logrotater
[snyk-image]: https://snyk.io/test/npm/egg-logrotater/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-logrotater
[download-image]: https://img.shields.io/npm/dm/egg-logrotater.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-logrotater
[npm-image]: https://img.shields.io/npm/v/egg-logrotator.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-logrotator
[travis-image]: https://img.shields.io/travis/eggjs/egg-logrotator.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-logrotator
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-logrotator.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-logrotator?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-logrotator.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-logrotator
[snyk-image]: https://snyk.io/test/npm/egg-logrotator/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-logrotator
[download-image]: https://img.shields.io/npm/dm/egg-logrotator.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-logrotator

egg 的日志切割插件,默认会按照时间切割 `config.logger.rotateLogDirs` 目录下的日志文件。

Expand All @@ -27,14 +27,14 @@ egg 的日志切割插件,默认会按照时间切割 `config.logger.rotateLog
- `plugin.js`

```js
exports.logrotater = true;
exports.logrotator = true;
```

- `config.default.js`

```js
// 如果有需要按照文件大小切割的日志,在这里配置
exports.logrotater = {
exports.logrotator = {
filesRotateBySize: [], // 需要按大小切割的文件,其他日志文件仍按照通常方式切割
maxFileSize: 50 * 1024 * 1024, // 最大文件大小,默认为50m
maxFiles: 10, // 按大小切割时,文件最大切割的份数
Expand Down
15 changes: 8 additions & 7 deletions app/schedule/rotateByFile.js → app/schedule/rotate_by_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = app => {
}
}

const maxDays = app.config.logrotater.maxDays;
const maxDays = app.config.logrotator.maxDays;
if (maxDays && maxDays > 0) {
try {
yield logDirs.map(logdir => removeExpiredLogFiles(logdir, maxDays));
Expand All @@ -43,7 +43,7 @@ module.exports = app => {
}

// tell every one to reload logger
logger.info('[egg-logrotater] broadcast log-reload to workers, logDirs: %j', logDirs);
logger.info('[egg-logrotator] broadcast log-reload to workers, logDirs: %j', logDirs);
app.messenger.sendToApp('log-reload');
app.messenger.sendToAgent('log-reload');
};
Expand All @@ -57,19 +57,20 @@ module.exports = app => {
return;
}

logger.info(`[egg-logrotater] start rename files:${names} to ${logdir}/*.log${logname}`);
logger.info(`[egg-logrotator] start rename files:${names} to ${logdir}/*.log${logname}`);

yield names.map(name => function* () {
const logfile = path.join(logdir, name);
const newLogfile = logfile + logname;
const exists = yield fs.exists(newLogfile);
if (exists) {
return logger.error(`[egg-logrotater] logfile ${newLogfile} exists!!!`);
const err = new Error(`[egg-logrotator] logfile ${newLogfile} exists!!!`);
return logger.error(err);
}
try {
yield fs.rename(logfile, newLogfile);
} catch (err) {
err.message = `[egg-logrotater] rename logfile ${logfile} to ${newLogfile} ${err.message}`;
err.message = `[egg-logrotator] rename logfile ${logfile} to ${newLogfile}, ${err.message}`;
logger.error(err);
}
});
Expand All @@ -94,14 +95,14 @@ module.exports = app => {
return;
}

logger.info(`[egg-logrotater] start remove ${logdir} files: ${names.join(', ')}`);
logger.info(`[egg-logrotator] 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}`;
err.message = `[egg-logrotator] remove logfile ${logfile} error, ${err.message}`;
logger.error(err);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = app => {
const exports = {};

const logger = app.coreLogger;
const appLogrotatorConfig = app.config.logrotater;
const appLogrotatorConfig = app.config.logrotator;
const filesRotateBySize = appLogrotatorConfig.filesRotateBySize || [];
const maxFileSize = appLogrotatorConfig.maxFileSize;
const maxFiles = appLogrotatorConfig.maxFiles;
Expand All @@ -25,12 +25,12 @@ module.exports = app => {
try {
const stat = yield fs.stat(logfile);
if (stat.size >= maxFileSize) {
logger.info(`[egg-logrotater] file ${logfile} reach the maximum file size, current size: ${stat.size}, max size: ${maxFileSize}`);
logger.info(`[egg-logrotator] file ${logfile} reach the maximum file size, current size: ${stat.size}, max size: ${maxFileSize}`);
needSendMessage = true;
yield rotateBySize(logfile);
}
} catch (e) {
e.message = `[egg-logrotater] ${e.message}`;
e.message = `[egg-logrotator] ${e.message}`;
logger.error(e);
}
});
Expand Down Expand Up @@ -59,7 +59,7 @@ module.exports = app => {
// logfile => logfile.1
yield fs.rename(logfile, `${logfile}.1`);

logger.info('[egg-logrotater] broadcast egg-logrotater-reload to workers');
logger.info('[egg-logrotator] broadcast egg-logrotator-reload to workers');
}

// 如果文件存在,尝试备份,如果备份失败,直接删除文件。这个操作只会对按文件大小切分的场景生效。
Expand Down
6 changes: 3 additions & 3 deletions config/config.default.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

/**
* logrotater options
* @member Config#logrotater
* logrotator options
* @member Config#logrotator
* @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
*/
exports.logrotater = {
exports.logrotator = {
filesRotateBySize: [],
maxFileSize: 50 * 1024 * 1024,
maxFiles: 10,
Expand Down
31 changes: 15 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "egg-logrotater",
"name": "egg-logrotator",
"version": "1.0.1",
"description": "logrotater for egg",
"description": "logrotator for egg",
"eggPlugin": {
"name": "logrotater",
"name": "logrotator",
"dep": [
"schedule"
]
Expand All @@ -13,7 +13,6 @@
"plugin"
],
"dependencies": {
"co": "^4.6.0",
"moment": "^2.14.1",
"mz": "^2.4.0"
},
Expand All @@ -24,17 +23,17 @@
"app.js"
],
"devDependencies": {
"autod": "2",
"egg": "*",
"egg-bin": "1",
"egg-ci": "1",
"egg-mock": "*",
"eslint": "3",
"eslint-config-egg": "3",
"glob": "6",
"autod": "^2.7.0",
"egg": "test",
"egg-bin": "^1.2.0",
"egg-ci": "^1.0.3",
"egg-mock": "^0.0.4",
"eslint": "^3.3.1",
"eslint-config-egg": "^3.1.0",
"glob": "^7.0.5",
"pedding": "1",
"should": "8",
"supertest": "1"
"should": "^11.1.0",
"supertest": "^2.0.0"
},
"engines": {
"node": ">=4.0.0"
Expand All @@ -52,12 +51,12 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/eggjs/egg-logrotater.git"
"url": "git+https://github.com/eggjs/egg-logrotator.git"
},
"bugs": {
"url": "https://github.com/eggjs/egg/issues"
},
"homepage": "https://github.com/eggjs/egg-logrotater#readme",
"homepage": "https://github.com/eggjs/egg-logrotator#readme",
"author": "tianyi.jiangty",
"license": "MIT"
}
3 changes: 0 additions & 3 deletions test/fixtures/logrotater-app-size/package.json

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path');

module.exports = appInfo => {
const exports = {
logrotater: {
logrotator: {
filesRotateBySize: [ path.join(appInfo.baseDir, `logs/${appInfo.name}/egg-web.log`) ],
maxFileSize: 1,
maxFiles: 2,
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/logrotator-app-size/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "logrotator-app-size"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const path = require('path');

module.exports = () => {
const exports = {
logrotater: {
filesRotateBySize: [ path.join(__dirname, '../logs', 'logrotater', 'egg-web.log') ],
logrotator: {
filesRotateBySize: [ path.join(__dirname, '../logs', 'logrotator', 'egg-web.log') ],
maxFileSize: 1024,
maxFiles: 2,
rotateDuration: 30000,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "logrotater",
"name": "logrotator",
"dependencies": {
},
"private":true
Expand Down
Loading

0 comments on commit 0fe84da

Please sign in to comment.