2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
package logging
|
2014-05-22 05:05:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-11-04 04:14:56 +00:00
|
|
|
|
2022-04-14 22:51:00 +00:00
|
|
|
gsyslog "github.com/hashicorp/go-syslog"
|
2014-05-22 05:05:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// levelPriority is used to map a log level to a
|
|
|
|
// syslog priority level
|
|
|
|
var levelPriority = map[string]gsyslog.Priority{
|
|
|
|
"TRACE": gsyslog.LOG_DEBUG,
|
|
|
|
"DEBUG": gsyslog.LOG_INFO,
|
|
|
|
"INFO": gsyslog.LOG_NOTICE,
|
|
|
|
"WARN": gsyslog.LOG_WARNING,
|
2020-01-28 23:50:41 +00:00
|
|
|
"ERROR": gsyslog.LOG_ERR,
|
2014-05-22 05:05:36 +00:00
|
|
|
"CRIT": gsyslog.LOG_CRIT,
|
|
|
|
}
|
|
|
|
|
2015-09-15 12:22:08 +00:00
|
|
|
// SyslogWrapper is used to cleanup log messages before
|
2014-05-22 05:05:36 +00:00
|
|
|
// writing them to a Syslogger. Implements the io.Writer
|
|
|
|
// interface.
|
|
|
|
type SyslogWrapper struct {
|
2020-01-28 23:50:41 +00:00
|
|
|
l gsyslog.Syslogger
|
2014-05-22 05:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write is used to implement io.Writer
|
|
|
|
func (s *SyslogWrapper) Write(p []byte) (int, error) {
|
|
|
|
// Extract log level
|
|
|
|
var level string
|
|
|
|
afterLevel := p
|
|
|
|
x := bytes.IndexByte(p, '[')
|
|
|
|
if x >= 0 {
|
|
|
|
y := bytes.IndexByte(p[x:], ']')
|
|
|
|
if y >= 0 {
|
|
|
|
level = string(p[x+1 : x+y])
|
2022-04-14 22:51:00 +00:00
|
|
|
afterLevel = bytes.TrimLeft(p[x+y+2:], " ")
|
2014-05-22 05:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each log level will be handled by a specific syslog priority
|
|
|
|
priority, ok := levelPriority[level]
|
|
|
|
if !ok {
|
|
|
|
priority = gsyslog.LOG_NOTICE
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt the write
|
|
|
|
err := s.l.WriteLevel(priority, afterLevel)
|
|
|
|
return len(p), err
|
|
|
|
}
|