@boost/log
Version:
Lightweight level based logging system.
77 lines (70 loc) • 3.41 kB
JavaScript
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { FileTransport } from './FileTransport.mjs';
const DAYS_IN_WEEK = 7;
class RotatingFileTransport extends FileTransport {
constructor(...args) {
super(...args);
this.lastTimestamp = this.formatTimestamp(Date.now());
}
blueprint(schemas) {
const string = schemas.string;
return _objectSpread(_objectSpread({}, super.blueprint(schemas)), {}, {
rotation: string().oneOf(['hourly', 'daily', 'weekly', 'monthly'])
});
}
/**
* Format a `Date` object into a format used within the log file name.
*/
formatTimestamp(ms) {
const rotation = this.options.rotation;
const date = new Date(ms);
let timestamp = `${date.getFullYear()}${String(date.getMonth() + 1).padStart(2, '0')}`;
if (rotation === 'monthly') {
return timestamp;
}
// Special case, calculate the week manually and return,
// but do not append so other rotations inherit!
if (rotation === 'weekly') {
const firstDay = new Date(date.getFullYear(), date.getMonth(), 1).getDay();
const offsetDate = date.getDate() + firstDay - 1;
timestamp += `.W${Math.floor(offsetDate / DAYS_IN_WEEK) + 1}`;
return timestamp;
}
timestamp += String(date.getDate()).padStart(2, '0');
if (rotation === 'daily') {
return timestamp;
}
timestamp += `.${String(date.getHours()).padStart(2, '0')}`;
return timestamp;
}
/**
* @inheritDoc
*/
checkIfNeedsRotation() {
if (this.lastSize > this.options.maxSize || this.formatTimestamp(Date.now()) !== this.lastTimestamp) {
this.closeStreamAndRotateFile();
}
}
/**
* @inheritDoc
*/
getRotatedFileName() {
const name = this.path.name(true);
const ext = this.path.ext(true);
return `${name}-${this.lastTimestamp}.${ext}`;
}
/**
* @inheritDoc
*/
rotateFile() {
super.rotateFile();
// Update timestamp to the new format
this.lastTimestamp = this.formatTimestamp(Date.now());
}
}
export { RotatingFileTransport };
//# sourceMappingURL=RotatingFileTransport.mjs.map