blob: 2d64a7afc48df50ebf4873e6c7174fd64a18f1cc [file] [log] [blame]
Serge Bazanski5faa2fc2020-09-07 14:09:30 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package logtree
18
19import (
20 "fmt"
21 "time"
22)
23
Serge Bazanski1bfa0c22020-10-14 16:45:07 +020024// LeveledPayload is a log entry for leveled logs (as per leveled.go). It contains not only the log message itself and
25// its severity, but also additional metadata that would be usually seen in a text representation of a leveled log entry.
26type LeveledPayload struct {
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020027 // message is the log message, rendered from a leveled log call like Infof(), Warningf(), ...
28 message string
29 // timestamp is the time at which this message was emitted.
30 timestamp time.Time
31 // severity is the leveled Severity at which this message was emitted.
32 severity Severity
33 // file is the filename of the caller that emitted this message.
34 file string
35 // line is the line number within the file of the caller that emitted this message.
36 line int
37}
38
Serge Bazanski1bfa0c22020-10-14 16:45:07 +020039func (p *LeveledPayload) String() string {
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020040 // Same format as in glog:
41 // Lmmdd hh:mm:ss.uuuuuu threadid file:line]
42 // Except, threadid is (currently) always zero. In the future this field might be used for something different.
43
44 _, month, day := p.timestamp.Date()
45 hour, minute, second := p.timestamp.Clock()
46 nsec := p.timestamp.Nanosecond() / 1000
47
48 // TODO(q3k): rewrite this to printf-less code.
49 return fmt.Sprintf("%s%02d%02d %02d:%02d:%02d.%06d % 7d %s:%d] %s", p.severity, month, day, hour, minute, second,
50 nsec, 0, p.file, p.line, p.message)
51}
52
53// Message returns the inner message of this entry, ie. what was passed to the actual logging method.
Serge Bazanski1bfa0c22020-10-14 16:45:07 +020054func (p *LeveledPayload) Message() string { return p.message }
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020055
56// Timestamp returns the time at which this entry was logged.
Serge Bazanski1bfa0c22020-10-14 16:45:07 +020057func (p *LeveledPayload) Timestamp() time.Time { return p.timestamp }
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020058
59// Location returns a string in the form of file_name:line_number that shows the origin of the log entry in the
60// program source.
Serge Bazanski1bfa0c22020-10-14 16:45:07 +020061func (p *LeveledPayload) Location() string { return fmt.Sprintf("%s:%d", p.file, p.line) }
Serge Bazanski5faa2fc2020-09-07 14:09:30 +020062
63// Severity returns the Severity with which this entry was logged.
Serge Bazanski1bfa0c22020-10-14 16:45:07 +020064func (p *LeveledPayload) Severity() Severity { return p.severity }