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

filesize logrotate and log only specified level option #451

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,8 @@ var log = bunyan.createLogger({
type: 'rotating-file',
path: '/var/log/foo.log',
period: '1d', // daily rotation
count: 3 // keep 3 back copies
count: 3, // keep 3 back copies,
maxSizeKB: 1024 // rotate every 1024KB
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should add a proper explanation of what the "maxSizeKB" attribute does.
If you expand the code below, you'll see:

"Currently, there is no support for providing a template for the rotated files, or for rotating when the log reaches a threshold size."

Which is no longer true.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might also consider adding some instructions explaining how and where to set "logOnlyMinLevel" parameter.
I'm sorry if it's obvious and I didn't get it. :/

}]
});
```
Expand Down
29 changes: 25 additions & 4 deletions lib/bunyan.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ function isWritable(obj) {
* See README.md for full details.
* - `level`: set the level for a single output stream (cannot be used
* with `streams`)
* - `logOnlySetLevel`: Use level as the only level to log and not a
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't use any attribute "logOnlySetLevel".
Should it be "logOnlyMinLevel"?

* threshold (Default: false)
* - `stream`: the output stream for a logger with just one, e.g.
* `process.stdout` (cannot be used with `streams`)
* - `serializers`: object mapping log record field names to
Expand Down Expand Up @@ -398,6 +400,7 @@ function Logger(options, _childOptions, _childSimple) {
this._isSimpleChild = true;

this._level = parent._level;
this._logOnlyMinLevel = parent._logOnlyMinLevel;
this.streams = parent.streams;
this.serializers = parent.serializers;
this.src = parent.src;
Expand All @@ -419,6 +422,7 @@ function Logger(options, _childOptions, _childSimple) {
var self = this;
if (parent) {
this._level = parent._level;
this._logOnlyMinLevel = parent._logOnlyMinLevel;
this.streams = [];
for (var i = 0; i < parent.streams.length; i++) {
var s = objCopy(parent.streams[i]);
Expand All @@ -433,6 +437,7 @@ function Logger(options, _childOptions, _childSimple) {
}
} else {
this._level = Number.POSITIVE_INFINITY;
this._logOnlyMinLevel = false;
this.streams = [];
this.serializers = null;
this.src = false;
Expand Down Expand Up @@ -605,6 +610,7 @@ Logger.prototype.addStream = function addStream(s, defaultLevel) {
assert.ok(s.path);
assert.ok(mv, '"rotating-file" stream type is not supported: '
+ 'missing "mv" module');

s.stream = new RotatingFileStream(s);
if (!s.closeOnExit) {
s.closeOnExit = true;
Expand Down Expand Up @@ -1038,7 +1044,7 @@ function mkLogEmitter(minLevel) {
return (this._level <= minLevel);
} else if (this._level > minLevel) {
/* pass through */
} else {
} else if ((this._level === minLevel && this._logOnlyMinLevel) || (this._level <= minLevel && !this._logOnlyMinLevel)) {
rec = mkRecord(msgArgs);
str = this._emit(rec);
}
Expand Down Expand Up @@ -1176,9 +1182,16 @@ RotatingFileStream = function RotatingFileStream(options) {
this.path = options.path;

this.count = (options.count == null ? 10 : options.count);
this.maxSizeKB = (options.maxSizeKB == null ? 1024 * 512 /* 512MB */ : options.maxSizeKB);

assert.equal(typeof (this.count), 'number',
format('rotating-file stream "count" is not a number: %j (%s) in %j',
this.count, typeof (this.count), this));

assert.equal(typeof (this.maxSizeKB), 'number',
format('rotating-file stream "maxSizeKB" is not a number: %j (%s) in %j',
this.maxSizeKB, typeof (this.maxSizeKB), this));

assert.ok(this.count >= 0,
format('rotating-file stream "count" is not >= 0: %j in %j',
this.count, this));
Expand Down Expand Up @@ -1211,15 +1224,23 @@ RotatingFileStream = function RotatingFileStream(options) {
this.periodScope = 'd';
}

var fileSizeBytes = 0;
var lastModified = null;
var rotateAfterOpen = false;

try {
var fileInfo = fs.statSync(this.path);

fileSizeBytes = fileInfo.size;
lastModified = fileInfo.mtime.getTime();
} catch (err) {
this._debug(`Log file ${this.path} does not exist`);
}
catch (err) {
// file doesn't exist

if (fileSizeBytes / 1024 > this.maxSizeKB) {
rotateAfterOpen = true;
}
var rotateAfterOpen = false;

if (lastModified) {
var lastRotTime = this._calcRotTime(0);
if (lastModified < lastRotTime) {
Expand Down