Skip to content

Commit 834c500

Browse files
committed
add timestamp format option
1 parent 4833944 commit 834c500

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

examples/timestamps.d/timestamps.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ job "echoloop" {
88
stdout = "test.log"
99
stderr = "test_error.log"
1010
enableTimestamps = true
11+
timestampFormat = "test"
1112
}

internal/config/types.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ type BaseJobConfig struct {
9494
CanFail bool `hcl:"canFail" json:"canFail"`
9595
Controllable bool `hcl:"controllable" json:"controllable"`
9696
WorkingDirectory string `hcl:"workingDirectory" json:"workingDirectory,omitempty"`
97-
Stdout string `hcl:"stdout" json:"stdout,omitempty"`
98-
Stderr string `hcl:"stderr" json:"stderr,omitempty"`
99-
EnableTimestamps bool `hcl:"enableTimestamps" json:"enableTimestamps"`
97+
98+
// log config
99+
Stdout string `hcl:"stdout" json:"stdout,omitempty"`
100+
Stderr string `hcl:"stderr" json:"stderr,omitempty"`
101+
EnableTimestamps bool `hcl:"enableTimestamps" json:"enableTimestamps"`
102+
TimestampFormat string `hcl:"timestampFormat" json:"timestampFormat"` // defaults to RFC3339
100103
}
101104

102105
type Laziness struct {

pkg/proc/basejob.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ func (job *baseJob) startOnce(ctx context.Context, process chan<- *os.Process) e
115115
cmd.Env = os.Environ()
116116
cmd.Dir = job.Config.WorkingDirectory
117117

118-
// pipe stdout and stderr through timestamp function if they are enabled
118+
// pipe command's stdout and stderr through timestamp function if timestamps are enabled
119+
// otherwise just redirect stdout and err to job.stdout and job.stderr
119120
if job.Config.EnableTimestamps {
120121
stdoutPipe, err := cmd.StdoutPipe()
121122
if err != nil {
@@ -224,12 +225,21 @@ func (job *baseJob) closeStdFiles() {
224225
func (job *baseJob) logWithTimestamp(r io.Reader, w io.Writer) {
225226
l := log.WithField("job.name", job.Config.Name)
226227

228+
formatString, exists := TimeFormats[job.Config.TimestampFormat]
229+
if !exists {
230+
l.Warningf("unknown timestamp format %s, defaulting to RFC3339", job.Config.TimestampFormat)
231+
formatString = time.RFC3339
232+
}
233+
227234
scanner := bufio.NewScanner(r)
228235
for scanner.Scan() {
229-
timestamp := time.Now().Format(time.RFC3339)
236+
timestamp := time.Now().Format(formatString)
230237
line := fmt.Sprintf("[%s] %s\n", timestamp, scanner.Text())
231-
w.Write([]byte(line))
238+
if _, err := w.Write([]byte(line)); err != nil {
239+
l.Errorf("error writing log for process: %v\n", err)
240+
}
232241
}
242+
233243
if err := scanner.Err(); err != nil {
234244
l.Errorf("error reading from process: %v\n", err)
235245
}

pkg/proc/types.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,42 @@ package proc
22

33
import (
44
"context"
5-
"github.com/gorilla/mux"
6-
"github.com/gorilla/websocket"
75
"net/http"
86
"os"
97
"os/exec"
108
"sync"
119
"time"
1210

11+
"github.com/gorilla/mux"
12+
"github.com/gorilla/websocket"
13+
1314
"github.com/mittwald/mittnite/internal/config"
1415
)
1516

1617
const (
1718
ShutdownWaitingTimeSeconds = 10
1819
)
1920

21+
var TimeFormats = map[string]string{
22+
"RFC3339": time.RFC3339,
23+
"RFC3339Nano": time.RFC3339Nano,
24+
"RFC1123": time.RFC1123,
25+
"RFC1123Z": time.RFC1123Z,
26+
"RFC822": time.RFC822,
27+
"RFC822Z": time.RFC822Z,
28+
"ANSIC": time.ANSIC,
29+
"UnixDate": time.UnixDate,
30+
"RubyDate": time.RubyDate,
31+
"Kitchen": time.Kitchen,
32+
"Stamp": time.Stamp,
33+
"StampMilli": time.StampMilli,
34+
"StampMicro": time.StampMicro,
35+
"StampNano": time.StampNano,
36+
"DateTime": time.DateTime,
37+
"DateOnly": time.DateOnly,
38+
"TimeOnly": time.TimeOnly,
39+
}
40+
2041
type Runner struct {
2142
jobs []Job
2243
bootJobs []*BootJob

0 commit comments

Comments
 (0)