@@ -3,6 +3,8 @@ import date from "date-and-time";
33import path from "node:path" ;
44import { AppEnv } from "../env" ;
55import fs from "node:fs" ;
6+ import dayjs from "dayjs" ;
7+ import FileIndex from "../file" ;
68
79let fileName = null
810let fileStream = null
@@ -43,7 +45,7 @@ const cleanOldLogs = (keepDays: number) => {
4345 for ( let file of files ) {
4446 const filePath = path . join ( logDir , file )
4547 let date = null
46- for ( let s of file . split ( '_' ) ) {
48+ for ( let s of file . split ( / [ _ \\ . ] / ) ) {
4749 // 匹配 YYYYMMDD
4850 if ( s . match ( / ^ \d { 8 } $ / ) ) {
4951 date = s
@@ -117,13 +119,80 @@ const errorRenderOrMain = (label: string, data: any = null) => {
117119 }
118120}
119121
122+ const collectRenderOrMain = async ( option ?: {
123+ startTime ?: string ,
124+ endTime ?: string ,
125+ limit ?: number ,
126+ } ) => {
127+ option = Object . assign ( {
128+ startTime : dayjs ( ) . subtract ( 1 , 'day' ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
129+ endTime : dayjs ( ) . format ( 'YYYY-MM-DD HH:mm:ss' ) ,
130+ limit : 10 * 10000 ,
131+ } , option )
132+ let startMs = dayjs ( option . startTime ) . valueOf ( )
133+ let endMs = dayjs ( option . endTime ) . valueOf ( )
134+ let startDayMs = dayjs ( option . startTime ) . startOf ( 'day' ) . valueOf ( )
135+ let endDayMs = dayjs ( option . endTime ) . endOf ( 'day' ) . valueOf ( )
136+ let resultLines = [ ]
137+ let logFiles = [ ]
138+ logFiles = logFiles . concat ( await FileIndex . list ( logsDir ( ) , { isFullPath : true } ) )
139+ logFiles = logFiles . concat ( await FileIndex . list ( appLogsDir ( ) , { isFullPath : true } ) )
140+ // console.log('logFiles', logFiles)
141+ logFiles = logFiles . filter ( ( logFile ) => {
142+ if ( logFile . isDirectory ) {
143+ return false
144+ }
145+ let date = null
146+ for ( let s of logFile . name . split ( / [ _ \\ . ] / ) ) {
147+ // 匹配 YYYYMMDD
148+ if ( s . match ( / ^ \d { 8 } $ / ) ) {
149+ date = s
150+ break
151+ }
152+ }
153+ if ( ! date ) {
154+ return false
155+ }
156+ const fileDate = new Date (
157+ parseInt ( date . substring ( 0 , 4 ) ) ,
158+ parseInt ( date . substring ( 4 , 6 ) ) - 1 ,
159+ parseInt ( date . substring ( 6 , 8 ) )
160+ )
161+ if ( fileDate . getTime ( ) < startDayMs || fileDate . getTime ( ) > endDayMs ) {
162+ return false
163+ }
164+ return true
165+ } )
166+ // console.log('collectRenderOrMain', {
167+ // ...option,
168+ // logFiles, startMs, endMs, startDayMs, endDayMs
169+ // })
170+ for ( const logFile of logFiles ) {
171+ await FileIndex . readLine ( logFile . pathname , ( line ) => {
172+ const lineParts = line . split ( ' - ' )
173+ const lineTime = dayjs ( lineParts [ 0 ] )
174+ // console.log('lineTime', lineParts[0], lineTime.isBefore(startMs) || lineTime.isAfter(endMs))
175+ if ( lineTime . isBefore ( startMs ) || lineTime . isAfter ( endMs ) ) {
176+ return
177+ }
178+ resultLines . push ( line )
179+ } , { isFullPath : true } )
180+ }
181+ return {
182+ startTime : option . startTime ,
183+ endTime : option . endTime ,
184+ logs : resultLines . join ( "\n" ) ,
185+ }
186+ }
187+
120188
121189export default {
122190 root,
123191 info,
124192 error,
125193 infoRenderOrMain,
126194 errorRenderOrMain,
195+ collectRenderOrMain,
127196}
128197
129198export const Log = {
0 commit comments