-
Notifications
You must be signed in to change notification settings - Fork 518
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
}] | ||
}); | ||
``` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You didn't use any attribute "logOnlySetLevel". |
||
* 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 | ||
|
@@ -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; | ||
|
@@ -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]); | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
@@ -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)); | ||
|
@@ -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) { | ||
|
There was a problem hiding this comment.
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.