Include a CRC32-IEEE checksum in log lines (#4478)
Adds a CRC32-IEEE checksum to our log lines. At most this adds 8 bytes per line, and at least adds 2 bytes. Given this a relatively minor change I haven't bothered flagging it, although if we have anything in place that assumes the current structure of log lines we may want to add a flag in order to prevent immediate breakage before things can be altered. Fixes #4474.
This commit is contained in:
parent
d35c20db75
commit
f32fdc4639
14
log/log.go
14
log/log.go
|
@ -1,9 +1,12 @@
|
||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"hash/crc32"
|
||||||
"log/syslog"
|
"log/syslog"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
@ -117,6 +120,15 @@ type bothWriter struct {
|
||||||
clk clock.Clock
|
clk clock.Clock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LogLineChecksum(line string) string {
|
||||||
|
crc := crc32.ChecksumIEEE([]byte(line))
|
||||||
|
// Using the hash.Hash32 doesn't make this any easier
|
||||||
|
// as it also returns a uint32 rather than []byte
|
||||||
|
buf := make([]byte, binary.MaxVarintLen32)
|
||||||
|
binary.PutUvarint(buf, uint64(crc))
|
||||||
|
return base64.RawURLEncoding.EncodeToString(buf)
|
||||||
|
}
|
||||||
|
|
||||||
// Log the provided message at the appropriate level, writing to
|
// Log the provided message at the appropriate level, writing to
|
||||||
// both stdout and the Logger
|
// both stdout and the Logger
|
||||||
func (w *bothWriter) logAtLevel(level syslog.Priority, msg string) {
|
func (w *bothWriter) logAtLevel(level syslog.Priority, msg string) {
|
||||||
|
@ -126,6 +138,8 @@ func (w *bothWriter) logAtLevel(level syslog.Priority, msg string) {
|
||||||
const red = "\033[31m\033[1m"
|
const red = "\033[31m\033[1m"
|
||||||
const yellow = "\033[33m"
|
const yellow = "\033[33m"
|
||||||
|
|
||||||
|
msg = fmt.Sprintf("%s %s", LogLineChecksum(msg), msg)
|
||||||
|
|
||||||
switch syslogAllowed := int(level) <= w.syslogLevel; level {
|
switch syslogAllowed := int(level) <= w.syslogLevel; level {
|
||||||
case syslog.LOG_ERR:
|
case syslog.LOG_ERR:
|
||||||
if syslogAllowed {
|
if syslogAllowed {
|
||||||
|
|
|
@ -106,8 +106,8 @@ func ExampleLogger() {
|
||||||
impl.AuditErr("Error Audit")
|
impl.AuditErr("Error Audit")
|
||||||
impl.Warning("Warning Audit")
|
impl.Warning("Warning Audit")
|
||||||
// Output:
|
// Output:
|
||||||
// [31m[1mE000000 log.test [AUDIT] Error Audit[0m
|
// [31m[1mE000000 log.test 46_ghQg [AUDIT] Error Audit[0m
|
||||||
// [33mW000000 log.test Warning Audit[0m
|
// [33mW000000 log.test 9rr1xwQ Warning Audit[0m
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSyslogMethods(t *testing.T) {
|
func TestSyslogMethods(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue