
ADD: * `iox` subpackage FIX: * `logging` now has a way to return logWritier directly * added significant `io.*` interface compat to logWriter -- allowing a `logging.Logger` to essentially be used for a large amount of io interaction in other libraries.
210 lines
4.5 KiB
Go
210 lines
4.5 KiB
Go
package logging
|
|
|
|
import (
|
|
"unicode/utf8"
|
|
|
|
"r00t2.io/goutils/multierr"
|
|
)
|
|
|
|
/*
|
|
Close calls Logger.Shutdown() on the underlying Logger.
|
|
The Logger *must not be used* after this; it will need to be re-initialized with Logger.Setup()
|
|
or a new Logger (and thuse new logWriter) must be created to replace it.
|
|
|
|
It (along with logWriter.Write()) conforms to WriteCloser().
|
|
*/
|
|
func (l *logWriter) Close() (err error) {
|
|
|
|
if err = l.backend.Shutdown(); err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
Write writes bytes b to the underlying Logger's priority level if the logWriter's priority level(s) match.
|
|
It conforms to io.Writer. n will *always* == len(b) on success, because otherwise n would technically be >= len(b)
|
|
(if multiple priorities are enabled), which is undefined behavior per io.Writer.
|
|
|
|
b is converted to a string to normalize to the underlying Logger.
|
|
*/
|
|
func (l *logWriter) Write(b []byte) (n int, err error) {
|
|
|
|
var s string
|
|
var mErr *multierr.MultiError = multierr.NewMultiError(nil)
|
|
|
|
if b == nil {
|
|
return
|
|
}
|
|
|
|
s = string(b)
|
|
|
|
if l.prio.HasFlag(PriorityEmergency) {
|
|
if err = l.backend.Emerg(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityAlert) {
|
|
if err = l.backend.Alert(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityCritical) {
|
|
if err = l.backend.Crit(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityError) {
|
|
if err = l.backend.Err(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityWarning) {
|
|
if err = l.backend.Warning(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityNotice) {
|
|
if err = l.backend.Notice(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityInformational) {
|
|
if err = l.backend.Info(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityDebug) {
|
|
if err = l.backend.Debug(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
|
|
if !mErr.IsEmpty() {
|
|
err = mErr
|
|
return
|
|
}
|
|
|
|
n = len(b)
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
WriteByte conforms a logWriter to an io.ByteWriter. (It just wraps logWriter.Write().)
|
|
You should probably never use this; the logging overhead/prefix is going to be more data than the single byte itself.
|
|
|
|
c is converted to a string to normalize to the underlying Logger.
|
|
*/
|
|
func (l *logWriter) WriteByte(c byte) (err error) {
|
|
|
|
if _, err = l.Write([]byte{c}); err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
WriteRune follows the same signature of (bytes.Buffer).WriteRune() and (bufio.Writer).WriteRune(); thus if `io` ever defines an io.RuneWriter interface, here ya go.
|
|
|
|
n will *always* be equal to (unicode/utf8).RuneLen(r), unless r is an "invalid rune" -- in which case n will be 0 and err will be ErrInvalidRune..
|
|
*/
|
|
func (l *logWriter) WriteRune(r rune) (n int, err error) {
|
|
|
|
var b []byte
|
|
|
|
n = utf8.RuneLen(r)
|
|
if n < 0 {
|
|
err = ErrInvalidRune
|
|
n = 0
|
|
return
|
|
}
|
|
|
|
b = make([]byte, n)
|
|
utf8.EncodeRune(b, r)
|
|
|
|
if n, err = l.Write(b); err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
/*
|
|
WriteString writes string s to the underlying Logger's priority level if the logWriter's priority level(s) match.
|
|
It conforms to io.StringWriter. n will *always* == len(s) on success, because otherwise n would technically be >= len(s)
|
|
(if multiple priorities are enabled), which is undefined behavior per io.StringWriter.
|
|
*/
|
|
func (l *logWriter) WriteString(s string) (n int, err error) {
|
|
|
|
var mErr *multierr.MultiError = multierr.NewMultiError(nil)
|
|
|
|
if l.prio.HasFlag(PriorityEmergency) {
|
|
if err = l.backend.Emerg(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityAlert) {
|
|
if err = l.backend.Alert(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityCritical) {
|
|
if err = l.backend.Crit(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityError) {
|
|
if err = l.backend.Err(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityWarning) {
|
|
if err = l.backend.Warning(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityNotice) {
|
|
if err = l.backend.Notice(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityInformational) {
|
|
if err = l.backend.Info(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
if l.prio.HasFlag(PriorityDebug) {
|
|
if err = l.backend.Debug(s); err != nil {
|
|
mErr.AddError(err)
|
|
err = nil
|
|
}
|
|
}
|
|
|
|
if !mErr.IsEmpty() {
|
|
err = mErr
|
|
return
|
|
}
|
|
|
|
n = len(s)
|
|
|
|
return
|
|
}
|