@@ -11,14 +11,15 @@ const depd = require('depd')('egg-logger');
1111const utils = require ( 'egg-logger/lib/utils' ) ;
1212
1313import { Transport } from 'egg-logger' ;
14+ import { consoleFormatter } from './format' ;
1415
1516/**
1617 * output log into file {@link Transport}。
1718 */
1819export class FileTransport extends Transport {
1920 _stream : fs . WriteStream | null ;
2021 options : any ;
21- logCount : number = 1 ;
22+
2223 /**
2324 * @constructor
2425 * @param {Object } options
@@ -31,7 +32,6 @@ export class FileTransport extends Transport {
3132 assert ( this . options . sizeLimit , 'should pass options.sizeLimit' ) ;
3233
3334 this . _stream = null ;
34- this . logCount = 1 ;
3535 this . reload ( ) ;
3636 }
3737
@@ -65,13 +65,13 @@ export class FileTransport extends Transport {
6565 // 存在,则判断是否溢出
6666 if ( filename ) {
6767 const overflow = this . checkSizeOverflow ( filename ) ;
68- // 如果溢出,logCount ++ , reload
68+ // 如果溢出,reload
6969 if ( overflow ) {
70- this . logCount += 1 ;
70+ // filename重命名
71+ this . renameLogFile ( filename ) ;
7172 this . reload ( ) ;
7273 }
7374 } else {
74- this . logCount = 1 ;
7575 this . reload ( ) ;
7676 }
7777
@@ -80,19 +80,33 @@ export class FileTransport extends Transport {
8080 console . error ( err . stack ) ;
8181 return ;
8282 }
83+ meta = meta || { } ;
84+ meta . formatter = consoleFormatter ;
8385 const buf = super . log ( level , args , meta ) ;
8486 // @ts -ignore
8587 if ( buf . length ) {
8688 this . _write ( buf ) ;
8789 }
8890 }
8991
92+ renameLogFile ( filename : string ) {
93+ const today = dayjs ( ) ;
94+ const dir = path . dirname ( filename ) ;
95+ const mill = today . format ( 'HH:mm:ss' ) ;
96+ const refilename = path . join (
97+ dir ,
98+ `${ today . format ( 'YYYY-MM-DD' ) } -${ mill } .log`
99+ ) ;
100+ fs . renameSync ( filename , refilename ) ;
101+ }
102+
90103 /**
91104 * 检查当前的日志文件是否为当天
92105 */
93106 checkIsPresent ( ) {
94107 // 检查前面的日志
95- // 2019-05-29.1.log
108+ // 2019-06-01-21:29:01.log
109+ // 而且检查当前文件夹
96110 const filename = this . getPresentFilename ( ) ;
97111 const exist = fs . existsSync ( filename ) ;
98112 if ( exist ) {
@@ -106,8 +120,10 @@ export class FileTransport extends Transport {
106120 const dir : string = path . isAbsolute ( this . options . dir )
107121 ? this . options . dir
108122 : path . join ( process . cwd ( ) , this . options . dir ) ;
109- const today = dayjs ( ) . format ( 'YYYY-MM-DD' ) ;
110- const filename = path . join ( dir , `${ today } .${ this . logCount } .log` ) ;
123+ const today = dayjs ( ) ;
124+ const ddir = path . join ( dir , today . format ( 'YYYY-MM' ) ) ;
125+ const dfilename = today . format ( 'YYYY-MM-DD' ) ;
126+ const filename = path . join ( ddir , `${ dfilename } .log` ) ;
111127 return filename ;
112128 }
113129
0 commit comments