Skip to content

Commit

Permalink
chore: 更换tail实现
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Jun 8, 2023
1 parent fb95f54 commit e4bb2e3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
29 changes: 28 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"morgan": "^1.9.1",
"ms": "^2.1.2",
"progress": "^2.0.3",
"socket.io-client": "^4.5.1"
"socket.io-client": "^4.5.1",
"tail": "^2.2.6"
},
"devDependencies": {
"@bangbang93/eslint-config-recommended": "^0.0.3",
Expand All @@ -50,6 +51,7 @@
"@types/ms": "^0.7.30",
"@types/node": "^18.11.17",
"@types/progress": "^2.0.5",
"@types/tail": "^2.2.1",
"@typescript-eslint/eslint-plugin": "^5.47.0",
"@typescript-eslint/parser": "^5.47.0",
"esbuild": "^0.17.18",
Expand Down
14 changes: 6 additions & 8 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import {tmpdir} from 'os'
import {dirname, join, sep} from 'path'
import {cwd} from 'process'
import ProgressBar from 'progress'
import {createInterface} from 'readline'
import {connect, Socket} from 'socket.io-client'
import {Tail} from 'tail'
import {validateFile} from './file.js'
import MeasureRoute from './measure.route.js'

Expand Down Expand Up @@ -212,17 +212,15 @@ export class Cluster {
stdio: [null, logFd, 'inherit'],
})

const tail = spawn('tail', ['-f', logFile])
const rl = createInterface({
input: tail.stdout,
})

const tail = new Tail(logFile)
if (!process.env.DISABLE_ACCESS_LOG) {
tail.stdout.pipe(process.stdout)
tail.on('line', (line: string) => {
process.stdout.write(line)
})
}
// eslint-disable-next-line max-len
const logRegexp = /^(?<client>\S+) \S+ (?<userid>\S+) \[(?<datetime>[^\]]+)] "(?<method>[A-Z]+) (?<request>[^ "]+)? HTTP\/[0-9.]+" (?<status>[0-9]{3}) (?<size>[0-9]+|-) "(?<referrer>[^"]*)" "(?<useragent>[^"]*)"/
rl.on('line', (line: string) => {
tail.on('line', (line: string) => {
const match = line.match(logRegexp)
if (!match) {
console.log(`cannot parse nginx log: ${line}`)
Expand Down

0 comments on commit e4bb2e3

Please sign in to comment.