Skip to content

Commit

Permalink
fix: remove winston-daily-rotate-file (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored May 7, 2021
1 parent 4077c70 commit ae242c1
Show file tree
Hide file tree
Showing 7 changed files with 522 additions and 4 deletions.
7 changes: 5 additions & 2 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
"main": "dist/index",
"typings": "dist/index.d.ts",
"dependencies": {
"file-stream-rotator": "^0.5.7",
"object-hash": "^2.0.1",
"triple-beam": "^1.3.0",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.0",
"winston-transport": "^4.4.0"
"winston-transport": "^4.4.0",
"zlib": "^1.0.5"
},
"devDependencies": {
"dayjs": "^1.10.4",
"egg-logger": "^2.4.2",
"fs-extra": "^8.0.1"
},
Expand Down
77 changes: 76 additions & 1 deletion packages/logger/src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as logform from 'logform';
import TransportStream = require('winston-transport');

export interface ILogger {
info(msg: any, ...args: any[]): void;
Expand All @@ -7,7 +8,6 @@ export interface ILogger {
warn(msg: any, ...args: any[]): void;
}


export type LoggerCustomInfoHandler = (
info: MidwayTransformableInfo
) => MidwayTransformableInfo;
Expand Down Expand Up @@ -82,3 +82,78 @@ export interface MidwayTransformableInfo {
originError?: Error;
stack?: string;
}

export interface GeneralDailyRotateFileTransportOptions extends TransportStream.TransportStreamOptions {
json?: boolean;
eol?: string;

/**
* A string representing the moment.js date format to be used for rotating. The meta characters used in this string will dictate the frequency of the file rotation. For example, if your datePattern is simply 'HH' you will end up with 24 log files that are picked up and appended to every day. (default 'YYYY-MM-DD')
*/
datePattern?: string;

/**
* A boolean to define whether or not to gzip archived log files. (default 'false')
*/
zippedArchive?: boolean;

/**
* Filename to be used to log to. This filename can include the %DATE% placeholder which will include the formatted datePattern at that point in the filename. (default: 'winston.log.%DATE%)
*/
filename?: string;

/**
* The directory name to save log files to. (default: '.')
*/
dirname?: string;

/**
* Write directly to a custom stream and bypass the rotation capabilities. (default: null)
*/
stream?: NodeJS.WritableStream;

/**
* Maximum size of the file after which it will rotate. This can be a number of bytes, or units of kb, mb, and gb. If using the units, add 'k', 'm', or 'g' as the suffix. The units need to directly follow the number. (default: null)
*/
maxSize?: string | number;

/**
* Maximum number of logs to keep. If not set, no logs will be removed. This can be a number of files or number of days. If using days, add 'd' as the suffix. (default: null)
*/
maxFiles?: string | number;

/**
* An object resembling https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options indicating additional options that should be passed to the file stream. (default: `{ flags: 'a' }`)
*/
options?: string | object;

/**
* A string representing the name of the name of the audit file. (default: './hash-audit.json')
*/
auditFile?: string;

/**
* A string representing the frequency of rotation. (default: 'custom')
*/
frequency?: string;

/**
* A boolean whether or not to generate file name from "datePattern" in UTC format. (default: false)
*/
utc?: boolean;

/**
* A string representing an extension to be added to the filename, if not included in the filename property. (default: '')
*/
extension?: string;

/**
* Create a tailable symlink to the current active log file. (default: false)
*/
createSymlink?: boolean;

/**
* The name of the tailable symlink. (default: 'current.log')
*/
symlinkName?: string;
}
2 changes: 1 addition & 1 deletion packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createLogger, transports, Logger, format } from 'winston';
import * as DailyRotateFileTransport from 'winston-daily-rotate-file';
import { DailyRotateFileTransport } from './rotate';
import {
DelegateLoggerOptions,
LoggerLevel,
Expand Down
176 changes: 176 additions & 0 deletions packages/logger/src/rotate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as zlib from 'zlib';
import * as hash from 'object-hash';
import { MESSAGE } from 'triple-beam';
import { PassThrough } from 'stream';
import Transport = require('winston-transport');
import rotator = require('file-stream-rotator');
import { GeneralDailyRotateFileTransportOptions } from './interface';

const loggerDefaults = {
json: false,
colorize: false,
eol: os.EOL,
logstash: null,
prettyPrint: false,
label: null,
stringify: false,
depth: null,
showLevel: true,
timestamp: function () {
return new Date().toISOString();
},
};

const noop = function () {};

function isValidFileName(filename) {
// eslint-disable-next-line no-control-regex
return !/["<>|:*?\\/\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f]/g.test(
filename
);
}

function isValidDirName(dirname) {
// eslint-disable-next-line no-control-regex
return !/["<>|\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f]/g.test(
dirname
);
}

function getMaxSize(size) {
if (size && typeof size === 'string') {
const _s = size.toLowerCase().match(/^((?:0\.)?\d+)([k|m|g])$/);
if (_s) {
return size;
}
} else if (size && Number.isInteger(size)) {
const sizeK = Math.round(size / 1024);
return sizeK === 0 ? '1k' : sizeK + 'k';
}

return null;
}

function throwIf(options, ...args) {
Array.prototype.slice.call(args, 1).forEach(name => {
if (options[name]) {
throw new Error('Cannot set ' + name + ' and ' + args[0] + ' together');
}
});
}

export class DailyRotateFileTransport extends Transport {
options;
logStream;
name = 'dailyRotateFile';
dirname: string;
filename: string;

constructor(options: GeneralDailyRotateFileTransportOptions = {}) {
super(options);
this.options = Object.assign({}, loggerDefaults, options);

if (options.stream) {
throwIf(options, 'stream', 'filename', 'maxsize');
this.logStream = new PassThrough();
this.logStream.pipe(options.stream);
} else {
this.filename = options.filename
? path.basename(options.filename)
: 'winston.log';
this.dirname = options.dirname || path.dirname(options.filename);

if (!isValidFileName(this.filename) || !isValidDirName(this.dirname)) {
throw new Error('Your path or filename contain an invalid character.');
}

this.logStream = rotator.getStream({
filename: path.join(this.dirname, this.filename),
frequency: options.frequency ? options.frequency : 'custom',
date_format: options.datePattern ? options.datePattern : 'YYYY-MM-DD',
verbose: false,
size: getMaxSize(options.maxSize),
max_logs: options.maxFiles,
end_stream: true,
audit_file: options.auditFile
? options.auditFile
: path.join(this.dirname, '.' + hash(options) + '-audit.json'),
file_options: options.options ? options.options : { flags: 'a' },
utc: options.utc ? options.utc : false,
extension: options.extension ? options.extension : '',
create_symlink: options.createSymlink ? options.createSymlink : false,
symlink_name: options.symlinkName ? options.symlinkName : 'current.log',
});

this.logStream.on('new', newFile => {
this.emit('new', newFile);
});

this.logStream.on('rotate', (oldFile, newFile) => {
this.emit('rotate', oldFile, newFile);
});

this.logStream.on('logRemoved', params => {
if (options.zippedArchive) {
const gzName = params.name + '.gz';
if (fs.existsSync(gzName)) {
try {
fs.unlinkSync(gzName);
} catch (_err) {
// file is there but we got an error when trying to delete,
// so permissions problem or concurrency issue and another
// process already deleted it we could detect the concurrency
// issue by checking err.type === ENOENT or EACCESS for
// permissions ... but then?
}
this.emit('logRemoved', gzName);
return;
}
}
this.emit('logRemoved', params.name);
});

if (options.zippedArchive) {
this.logStream.on('rotate', oldFile => {
const oldFileExist = fs.existsSync(oldFile);
const gzExist = fs.existsSync(oldFile + '.gz');
if (!oldFileExist || gzExist) {
return;
}

const gzip = zlib.createGzip();
const inp = fs.createReadStream(oldFile);
const out = fs.createWriteStream(oldFile + '.gz');
inp
.pipe(gzip)
.pipe(out)
.on('finish', () => {
if (fs.existsSync(oldFile)) {
fs.unlinkSync(oldFile);
}
this.emit('archive', oldFile + '.gz');
});
});
}
}
}

log(info, callback) {
callback = callback || noop;

this.logStream.write(info[MESSAGE] + this.options.eol);
this.emit('logged', info);
callback(null, true);
}

close() {
if (this.logStream) {
this.logStream.end(() => {
this.emit('finish');
});
}
}
}
33 changes: 33 additions & 0 deletions packages/logger/test/memory-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Writable } from 'stream';

export class MemoryStream extends Writable {

_writableState;

write(...args) {
const ret = Writable.prototype.write.apply(this, args);
if (!ret) {
this.emit('drain');
}

return ret;
}

_write(chunk, encoding, callback) {
this.write(chunk, encoding, callback);
}

toString() {
return this.toBuffer().toString();
}

toBuffer() {
const buffers = [];
this._writableState.getBuffer().forEach(function (data) {
buffers.push(data.chunk);
});

return Buffer.concat(buffers);
};

}
31 changes: 31 additions & 0 deletions packages/logger/test/random-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const uppers = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const lowers = 'abcdefghijklmnopqrstuvwxyz';
const numbers = '0123456789';
const specials = '_-|@.,?/!~#$%^&*(){}[]+=';
const charClasses = [uppers, lowers, numbers, specials];
const minLen = charClasses.length;
function chooseRandom(x) {
const i = Math.floor(Math.random() * x.length);
return (typeof (x) === 'string') ? x.substr(i, 1) : x[i];
}

export const randomString = (maxLen) => {
maxLen = (maxLen || 36);
if (maxLen < minLen) {
throw new Error('length must be >= ' + minLen);
}

let str = '';
const usedClasses = {};
let charClass;
do { // Append a random char from a random char class.
while (str.length < maxLen) {
charClass = chooseRandom(charClasses);
usedClasses[charClass] = true;
str += chooseRandom(charClass);
}
// Ensure we have picked from every char class.
} while (Object.keys(usedClasses).length !== charClasses.length);

return str;
}
Loading

0 comments on commit ae242c1

Please sign in to comment.