blob: c6707e919e317ab1b78c4f9e629d65f92ea32876 [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
24// Payload is a log entry for leveled logs (as per leveled.go). It contains not only the log message itself and its
25// severity, but also additional metadata that would be usually seen in a text representation of a leveled log entry.
26type Payload struct {
27 // 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
39func (p *Payload) String() string {
40 // 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.
54func (p *Payload) Message() string { return p.message }
55
56// Timestamp returns the time at which this entry was logged.
57func (p *Payload) Timestamp() time.Time { return p.timestamp }
58
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.
61func (p *Payload) Location() string { return fmt.Sprintf("%s:%d", p.file, p.line) }
62
63// Severity returns the Severity with which this entry was logged.
64func (p *Payload) Severity() Severity { return p.severity }