Skip to content

Commit 38d4bde

Browse files
author
anahan
committed
Add phpfpm.ErrLog parser
1 parent fe7c915 commit 38d4bde

4 files changed

Lines changed: 84 additions & 2 deletions

File tree

pkg/phpfpm/errlog_parser.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package phpfpm
2+
3+
import (
4+
"bufio"
5+
"context"
6+
"errors"
7+
"fmt"
8+
"io"
9+
"regexp"
10+
"time"
11+
12+
"github.com/code-tool/docker-fpm-wrapper/pkg/line"
13+
)
14+
15+
type ErrLogEntry struct {
16+
CreatedAt time.Time
17+
Level LogLevel
18+
Message string
19+
}
20+
21+
type ErrLogParser struct {
22+
//
23+
}
24+
25+
func NewErrLogParser() *ErrLogParser {
26+
return &ErrLogParser{}
27+
}
28+
29+
var errLogEntryRegexp = regexp.MustCompile(`^\[([^]]+)]\s+(ALERT|ERROR|WARNING|NOTICE|DEBUG):\s+([^\n]+)\n$`)
30+
31+
func (p *ErrLogParser) ParseOne(r *bufio.Reader) (ErrLogEntry, error) {
32+
result := ErrLogEntry{}
33+
buf, err := line.ReadOne(r)
34+
if err != nil {
35+
return result, err
36+
}
37+
38+
matches := errLogEntryRegexp.FindSubmatchIndex(buf)
39+
if len(matches) == 0 {
40+
return result, errors.New("unexpected log line format")
41+
}
42+
//
43+
result.CreatedAt, err = time.Parse("02-Jan-2006 15:04:05", string(buf[matches[2]:matches[3]]))
44+
if err != nil {
45+
return result, fmt.Errorf("can't parse timestamp: %w", err)
46+
}
47+
48+
result.Level = LogLevel(buf[matches[4]:matches[5]])
49+
result.Message = string(buf[matches[6]:matches[7]])
50+
51+
return result, nil
52+
}
53+
54+
func (p *ErrLogParser) Parse(ctx context.Context, r io.Reader, out chan ErrLogEntry) error {
55+
bufioReader := bufio.NewReader(r)
56+
57+
for {
58+
entry, err := p.ParseOne(bufioReader)
59+
if err != nil {
60+
return err
61+
}
62+
63+
select {
64+
case <-ctx.Done():
65+
return nil
66+
default:
67+
out <- entry
68+
}
69+
}
70+
}

pkg/phpfpm/log.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package phpfpm
2+
3+
type LogLevel string
4+
5+
const (
6+
LogLevelAlert = "ALERT"
7+
LogLevelError = "ERROR"
8+
LogLevelWarning = "WARNING"
9+
LogLevelNotice = "NOTICE"
10+
LogLevelDebug = "DEBUG"
11+
)
12+
13+
const logTimeFormat = "02-Jan-2006 15:04:05"

pkg/phpfpm/slowlog_entry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (se *SlowlogEntry) String() string {
3535
b strings.Builder
3636
)
3737

38-
_, err = fmt.Fprintf(&b, "[%s] [pool %s] pid %d\n", se.CreatedAt.Format(slowlogTimeFormat), se.PoolName, se.Pid)
38+
_, err = fmt.Fprintf(&b, "[%s] [pool %s] pid %d\n", se.CreatedAt.Format(logTimeFormat), se.PoolName, se.Pid)
3939
if err != nil {
4040
return ""
4141
}

pkg/phpfpm/slowlog_parser.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const (
2020
)
2121

2222
const stacktraceParsingTimeout = 25 * time.Millisecond
23-
const slowlogTimeFormat = "02-Jan-2006 15:04:05"
2423

2524
var (
2625
headerRegexp = regexp.MustCompile(`^\[([^]]+)]\s+\[pool\s([^]]+)]\s+pid\s+(\d+)(?:[\r\n]|$)`)

0 commit comments

Comments
 (0)