Documentation
¶
Overview ¶
Package log is an important part of the application and having a consistent logging mechanism and structure is mandatory. With several teams writing different components that talk to each other, being able to read each others logs could be the difference between finding bugs quickly or wasting hours.
Loggers ¶
With the log package we have the ability to create custom loggers that can be configured to write to one or many devices. This not only simplifies things, but will keep each log trace in correct sequence.
Logging levels ¶
This package includes logging levels. Not everything needs to be logged all the time, so logging can be performed on a module/package basis.
Tracing Formats ¶
There are two types of tracing lines we need to log. One is a trace line that describes where the program is, what it is doing and any data associated with that trace. The second is formatted data such as a JSON document or binary dump of data. Each serve a different purpose but they both exists within the same scope of space and time.
The format of each trace line needs to be consistent and helpful or else the logging will just be noise and ultimately useless.
YYYY/MM/DD HH:MM:SS.ZZZ: APP[PID]: file.go#LN: Context: Func: Tag: Var[value]: Messages
Here is a breakdown of each section and a sample value:
YYYY/MM/DD Date of the trace log line in UTC. Ex. 2015/03/23 HH:MM:SS.ZZZZZZ Time of the trace log line with microsecond in UTC. Ex. 14:02:42.123 APP The application or service name. Set on Init. PID The process id for the running program. file.go#LN The name of the source code file and line number. Ex. main.go#15 Context: Any context that is passed to the logging function. Func: The name of the function writing the trace log. Tag: The tag of trace line. Started: Start of a function/method call. Completed: Return of a function/method call. Completed ERROR: Return of a function/method call with error. ERROR: Error trace. TERMINATING: Termination of the application. Trace: All messages. Warning: Warning trace. Query: Any query that can be copied/pasted and run. DATA: A dump of data Var[value]: Optional, Data values, parameters or return values. Ex. ID[1234] Messages: Optional, Any extended information with proper grammar. Ex. Waiting on SMSC to acknowledge request.
Here are examples of how trace lines would show in the log:
2009/11/10 15:00:00.000: EXAMPLE[69910]: file.go#512: 1234: Basic: Started: 2009/11/10 15:00:00.000: EXAMPLE[69910]: file.go#512: 1234: Basic: Completed: Conv[10]
API Documentation and Examples ¶
The API for the log package is focused on initializing the logger and then provides function abstractions for the different tags we have defined.
Example (MultipleInit) ¶
Example_multipleInit tests that when Init is called back to back, the log flushes and takes the change.
package main import ( "fmt" "io/ioutil" "github.com/Comcast/go-log/log" ) func main() { // Init the log system using a buffer for testing. buf := new(log.SafeBuffer) log.InitTest("EXAMPLE", 10, log.DevWriter{Device: log.DevAll, Writer: buf}) log.Start("1234", "Basic") log.Complete("1234", "Basic") // Now init the log with a buffer to disable logging log.InitTest("EXAMPLE", 10, log.DevWriter{Device: log.DevAll, Writer: ioutil.Discard}) log.Start("1234", "SHOULD NOT BE DISPLAYED") log.Complete("1234", "SHOULD NOT BE DISPLAYED") log.Shutdown() fmt.Println(buf.String()) }
Output: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Basic: Started: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Basic: Completed:
Index ¶
- Constants
- Variables
- func Complete(context interface{}, function string)
- func CompleteErr(err error, context interface{}, function string)
- func CompleteErrf(err error, context interface{}, function string, format string, ...)
- func Completef(context interface{}, function string, format string, a ...interface{})
- func DataBlock(context interface{}, function string, block interface{})
- func DataKV(context interface{}, function string, key string, value interface{})
- func DataString(context interface{}, function string, message string)
- func DataTrace(context interface{}, function string, formatters ...Formatter)
- func Err(err error, context interface{}, function string)
- func ErrFatal(err error, context interface{}, function string)
- func ErrFatalf(err error, context interface{}, function string, format string, ...)
- func ErrPanic(err error, context interface{}, function string)
- func ErrPanicf(err error, context interface{}, function string, format string, ...)
- func Errf(err error, context interface{}, function string, format string, ...)
- func GetBulkLogPeriod() time.Duration
- func Init(prefix string, bufferSize int, dws ...DevWriter)
- func InitTest(prefix string, bufferSize int, dws ...DevWriter)
- func Queryf(context interface{}, function string, format string, a ...interface{})
- func SetBulkLogPeriod(p time.Duration)
- func SetStallTimeout(t time.Duration)
- func Shutdown()
- func Splunk(m ...SplunkPair)
- func Start(context interface{}, function string)
- func Startf(context interface{}, function string, format string, a ...interface{})
- func Tracef(context interface{}, function string, format string, a ...interface{})
- func Warnf(context interface{}, function string, format string, a ...interface{})
- type DevWriter
- type Formatter
- type Logger
- func (l *Logger) Complete(context interface{}, function string)
- func (l *Logger) CompleteErr(err error, context interface{}, function string)
- func (l *Logger) CompleteErrf(err error, context interface{}, function string, format string, ...)
- func (l *Logger) Completef(context interface{}, function string, format string, a ...interface{})
- func (l *Logger) DataBlock(context interface{}, function string, block interface{})
- func (l *Logger) DataKV(context interface{}, function string, key string, value interface{})
- func (l *Logger) DataString(context interface{}, function string, message string)
- func (l *Logger) DataTrace(context interface{}, function string, formatters ...Formatter)
- func (l *Logger) Err(err error, context interface{}, function string)
- func (l *Logger) ErrFatal(err error, context interface{}, function string)
- func (l *Logger) ErrFatalf(err error, context interface{}, function string, format string, ...)
- func (l *Logger) ErrPanic(err error, context interface{}, function string)
- func (l *Logger) ErrPanicf(err error, context interface{}, function string, format string, ...)
- func (l *Logger) Errf(err error, context interface{}, function string, format string, ...)
- func (l *Logger) Queryf(context interface{}, function string, format string, a ...interface{})
- func (l *Logger) Start(context interface{}, function string)
- func (l *Logger) Startf(context interface{}, function string, format string, a ...interface{})
- func (l *Logger) Tracef(context interface{}, function string, format string, a ...interface{})
- func (l *Logger) Warnf(context interface{}, function string, format string, a ...interface{})
- type SafeBuffer
- type SplunkPair
- type SplunkValue
- type Uplevel
- func (lvl Uplevel) Complete(context interface{}, function string)
- func (lvl Uplevel) CompleteErr(err error, context interface{}, function string)
- func (lvl Uplevel) CompleteErrf(err error, context interface{}, function string, format string, ...)
- func (lvl Uplevel) Completef(context interface{}, function string, format string, a ...interface{})
- func (lvl Uplevel) DataBlock(context interface{}, function string, block interface{})
- func (lvl Uplevel) DataKV(context interface{}, function string, key string, value interface{})
- func (lvl Uplevel) DataString(context interface{}, function string, message string)
- func (lvl Uplevel) DataTrace(context interface{}, function string, formatters ...Formatter)
- func (lvl Uplevel) Err(err error, context interface{}, function string)
- func (lvl Uplevel) ErrFatal(err error, context interface{}, function string)
- func (lvl Uplevel) ErrFatalf(err error, context interface{}, function string, format string, ...)
- func (lvl Uplevel) ErrPanic(err error, context interface{}, function string)
- func (lvl Uplevel) ErrPanicf(err error, context interface{}, function string, format string, ...)
- func (lvl Uplevel) Errf(err error, context interface{}, function string, format string, ...)
- func (lvl Uplevel) Queryf(context interface{}, function string, format string, a ...interface{})
- func (lvl Uplevel) Splunk(m ...SplunkPair)
- func (lvl Uplevel) Start(context interface{}, function string)
- func (lvl Uplevel) Startf(context interface{}, function string, format string, a ...interface{})
- func (lvl Uplevel) Tracef(context interface{}, function string, format string, a ...interface{})
- func (lvl Uplevel) Warnf(context interface{}, function string, format string, a ...interface{})
- type UplevelLogger
- func (lvl UplevelLogger) Complete(context interface{}, function string)
- func (lvl UplevelLogger) CompleteErr(err error, context interface{}, function string)
- func (lvl UplevelLogger) CompleteErrf(err error, context interface{}, function string, format string, ...)
- func (lvl UplevelLogger) Completef(context interface{}, function string, format string, a ...interface{})
- func (lvl UplevelLogger) DataBlock(context interface{}, function string, block interface{})
- func (lvl UplevelLogger) DataKV(context interface{}, function string, key string, value interface{})
- func (lvl UplevelLogger) DataString(context interface{}, function string, message string)
- func (lvl UplevelLogger) DataTrace(context interface{}, function string, formatters ...Formatter)
- func (lvl UplevelLogger) Err(err error, context interface{}, function string)
- func (lvl UplevelLogger) ErrFatal(err error, context interface{}, function string)
- func (lvl UplevelLogger) ErrFatalf(err error, context interface{}, function string, format string, ...)
- func (lvl UplevelLogger) ErrPanic(err error, context interface{}, function string)
- func (lvl UplevelLogger) ErrPanicf(err error, context interface{}, function string, format string, ...)
- func (lvl UplevelLogger) Errf(err error, context interface{}, function string, format string, ...)
- func (lvl UplevelLogger) Queryf(context interface{}, function string, format string, a ...interface{})
- func (lvl UplevelLogger) Start(context interface{}, function string)
- func (lvl UplevelLogger) Startf(context interface{}, function string, format string, a ...interface{})
- func (lvl UplevelLogger) Tracef(context interface{}, function string, format string, a ...interface{})
- func (lvl UplevelLogger) Warnf(context interface{}, function string, format string, a ...interface{})
Examples ¶
Constants ¶
const ( // DevAll will update all devices at the time it is applied. DevAll int8 = iota DevStart DevError DevPanic DevTrace DevWarning DevQuery DevData DevSplunk )
Set of constants that represent different trace lines types. Used to map different devices to the types.
const ( LevelOff = 0 LevelError = 1 LevelWarning = 2 LevelOutput = 3 LevelTrace = 4 )
Set of levels that are compared for filtering tracing to the specific log levels.
const (
LoggingWasOff = "**** LOG WARNING: LOGGING WAS OFF - PLEASE REPORT ****\n"
)
Date and time layout for each trace line.
Variables ¶
var Dev dev
Dev provides access to the set of device methods. The goal of this method set is to allow the library user to redirect certain method, like Err or Warning, to different devices, like StdErr or StdOut.
Functions ¶
func Complete ¶
func Complete(context interface{}, function string)
Complete is used for the exit of a function.
func CompleteErr ¶
CompleteErr is used to write an error with complete into the trace.
func CompleteErrf ¶
CompleteErrf is used to write an error with complete into the trace with a formatted message.
func DataBlock ¶
func DataBlock(context interface{}, function string, block interface{})
DataBlock is used to write a block of data into the trace.
Example ¶
ExampleDataBlock provides an example of logging a block of data.
package main import ( "fmt" "github.com/Comcast/go-log/log" ) func main() { // Init the log system using a buffer for testing. buf := new(log.SafeBuffer) log.InitTest("EXAMPLE", 10, log.DevWriter{Device: log.DevAll, Writer: buf}) { log.Start("1234", "Data_Block") data := `Test Data with 2 lines` log.DataBlock("1234", "Data_Block", data) log.Complete("1234", "Data_Block") } log.Shutdown() fmt.Println(buf.String()) }
Output: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Data_Block: Started: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Data_Block: DATA: Test Data with 2 lines 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Data_Block: Completed:
func DataKV ¶
DataKV is used to write a key/value pair into the trace.
Example ¶
ExampleDataKV provides an example of logging K/V pair data.
package main import ( "fmt" "github.com/Comcast/go-log/log" ) func main() { // Init the log system using a buffer for testing. buf := new(log.SafeBuffer) log.InitTest("EXAMPLE", 10, log.DevWriter{Device: log.DevAll, Writer: buf}) { log.Start("1234", "Data_KV") log.DataKV("1234", "Data_KV", "Value 1", 1) log.DataKV("1234", "Data_KV", "Hex Value 2", 0x00000002) log.Complete("1234", "Data_KV") } log.Shutdown() fmt.Println(buf.String()) }
Output: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Data_KV: Started: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Data_KV: DATA: Value 1: 1 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Data_KV: DATA: Hex Value 2: 2 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Data_KV: Completed:
func DataString ¶
DataString is used to write a string with CRLF each on their own line.
func DataTrace ¶
DataTrace is used to write a block of data from an io.Stringer respecting each line.
Example ¶
ExampleDataTrace provides an example of logging from a fmt.Stringer.
package main import ( "fmt" "github.com/Comcast/go-log/log" ) type Message []byte // Format implements the Formatter interface to produce logging output // for this slice of bytes. func (m Message) Format() string { var buf log.SafeBuffer fmt.Fprintf(&buf, "Message Bytes:\t") rows := (len(m) / 16) + 1 l := len(m) for row := 0; row < rows; row++ { var r []byte st := row * 16 if row < (rows - 1) { r = m[st : st+16] } else { r = m[st : st+(l-st)] } if row > 0 { fmt.Fprintf(&buf, "\t\t\t") } fmt.Fprintf(&buf, "(0x%.4X)", st) for i := 0; i < len(r); i++ { fmt.Fprintf(&buf, " %.2X", r[i]) } fmt.Fprintf(&buf, "\n") } return buf.String() } func main() { // Init the log system using a buffer for testing. buf := new(log.SafeBuffer) log.InitTest("EXAMPLE", 10, log.DevWriter{Device: log.DevAll, Writer: buf}) { log.Start("1234", "Data_String") b1 := []byte{0xEE, 0x6E, 0x11, 0x00, 0x00, 0x00, 0x3E, 0xEA, 0xDE, 0x18, 0x00, 0x00, 0x2D, 0x00, 0x00, 0x00, 0x3E, 0xEA, 0xDE} b2 := []byte{0xEE, 0x6E, 0x11, 0x00, 0x00, 0x00, 0x3E, 0xEA, 0xDE, 0x18, 0x00, 0x00, 0x2D, 0x00, 0x00, 0x00, 0x3E, 0xEA, 0xDE} log.DataTrace("1234", "Data_String", Message(b1), Message(b2)) log.Complete("1234", "Data_String") } log.Shutdown() fmt.Println(buf.String()) }
Output: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Data_String: Started: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Data_String: DATA: Message Bytes: (0x0000) EE 6E 11 00 00 00 3E EA DE 18 00 00 2D 00 00 00 (0x0010) 3E EA DE Message Bytes: (0x0000) EE 6E 11 00 00 00 3E EA DE 18 00 00 2D 00 00 00 (0x0010) 3E EA DE 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Data_String: Completed:
func Err ¶
Err is used to write an error into the trace.
Example ¶
ExampleErr provides an example of logging an error.
package main import ( "fmt" "strconv" "github.com/Comcast/go-log/log" ) func main() { // Init the log system using a buffer for testing. buf := new(log.SafeBuffer) log.InitTest("EXAMPLE", 10, log.DevWriter{Device: log.DevAll, Writer: buf}) { log.Start("1234", "Error") v, err := strconv.ParseInt("1080980980980980980898908", 10, 64) if err != nil { log.CompleteErr(err, "1234", "Error") // Flush the output for testing. log.Shutdown() fmt.Println(buf.String()) return } log.Completef("1234", "Error", "Conv[%d]", v) } log.Shutdown() fmt.Println(buf.String()) }
Output: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Error: Started: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Error: Completed ERROR: strconv.ParseInt: parsing "1080980980980980980898908": value out of range
func ErrFatalf ¶
ErrFatalf is used to write an error into the trace with a formatted message then terminate the program.
func ErrPanicf ¶
ErrPanicf is used to write an error into the trace with a formatted message then panic the program.
func GetBulkLogPeriod ¶
GetBulkLogPeriod retrieves the private value for the bulk log period.
func Init ¶
Init initializes the logging system for use. It can be called multiple times to reset the destination.
func SetBulkLogPeriod ¶
SetBulkLogPeriod sets the private value for the bulk log period.
func SetStallTimeout ¶
SetStallTimeout sets the stall timeout value.
func Splunk ¶
func Splunk(m ...SplunkPair)
Splunk is used to write a log message in a splunk-able format.
Example ¶
ExampleSplunk provides an example of logging a message in a splunk-able format.
package main import ( "fmt" "time" "github.com/Comcast/go-log/log" ) func main() { // Init the log system using a buffer for testing. buf := new(log.SafeBuffer) log.InitTest("TestSplunk", 10, log.DevWriter{Device: log.DevAll, Writer: buf}) sl1 := log.SplunkValue{1, 2, 3, 4} // slice of ints. sl2 := log.SplunkValue{"123.123", "123.124"} // slice of strings. sl3 := log.SplunkValue{6, "123.123"} // slice of mixed values. m := []log.SplunkPair{ {Key: "Key1", Value: "Value1"}, {Key: "RequestTime", Value: time.Date(2019, time.November, 10, 15, 0, 0, 0, time.UTC).UTC().Format("2006/01/02 15:04:05.000000000")}, {Key: "MAC", Value: "010203040506"}, {Key: "ResponseCode", Value: 0}, {Key: "Slice", Value: sl1}, {Key: "name1", Value: sl2}, {Key: "name2", Value: sl3}, } log.Splunk(m...) log.Splunk(log.SplunkPair{Key: "SecondKey", Value: "SecondValue"}, log.SplunkPair{Key: "RequestTime", Value: time.Date(2019, time.November, 10, 15, 0, 0, 0, time.UTC).UTC().Format("2006/01/02 15:04:05.000000000")}, log.SplunkPair{Key: "MAC", Value: "010203040507"}, log.SplunkPair{Key: "ResponseCode", Value: 0}, log.SplunkPair{Key: "Slice", Value: sl1}, log.SplunkPair{Key: "name1", Value: sl2}, log.SplunkPair{Key: "name2", Value: sl3}) log.Shutdown() fmt.Println(buf.String()) }
Output: 2009/11/10 15:00:00.000000000: Key1=Value1 RequestTime="2019/11/10 15:00:00.000000000" MAC=010203040506 ResponseCode=0 Slice=[1, 2, 3, 4] name1=[123.123, 123.124] name2=[6, 123.123] 2009/11/10 15:00:00.000000000: SecondKey=SecondValue RequestTime="2019/11/10 15:00:00.000000000" MAC=010203040507 ResponseCode=0 Slice=[1, 2, 3, 4] name1=[123.123, 123.124] name2=[6, 123.123]
func Start ¶
func Start(context interface{}, function string)
Start is used for the entry into a function.
Example ¶
ExampleStart provides a basic example for using the log package.
package main import ( "fmt" "strconv" "github.com/Comcast/go-log/log" ) func main() { // Init the log system using a buffer for testing. buf := new(log.SafeBuffer) log.InitTest("EXAMPLE", 10, log.DevWriter{Device: log.DevAll, Writer: buf}) { log.Start("1234", "Basic") v, err := strconv.ParseInt("10", 10, 64) if err != nil { log.CompleteErr(err, "1234", "Basic") return } log.Completef("1234", "Basic", "Conv[%d]", v) } log.Shutdown() fmt.Println(buf.String()) }
Output: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Basic: Started: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Basic: Completed: Conv[10]
func Tracef ¶
Tracef is used to write information into the trace with a formatted message.
Example ¶
ExampleTracef provides an example of logging from a fmt.Stringer and also tests newline handling.
package main import ( "fmt" "github.com/Comcast/go-log/log" ) func main() { // Init the log system using a buffer for testing. buf := new(log.SafeBuffer) log.InitTest("EXAMPLE", 10, log.DevWriter{Device: log.DevAll, Writer: buf}) { // Messages without a newline should have one added, and message // that have a newline should *not* have an extra one added. log.Tracef("1234", "Tracef", "%s: %s", "1234", "Basic") log.Tracef("1234", "Tracef", "%s: %s\n", "1234", "Basic") log.Tracef("1234", "Tracef", "%s: %s", "ABCD", "Basic") log.Tracef("1234", "Tracef", "%s: %s\n", "ABCD", "Basic") } log.Shutdown() fmt.Println(buf.String()) }
Output: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Tracef: Trace: 1234: Basic 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Tracef: Trace: 1234: Basic 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Tracef: Trace: ABCD: Basic 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Tracef: Trace: ABCD: Basic
Types ¶
type Formatter ¶
type Formatter interface {
Format() string
}
Formatter provide support for special formatting.
type Logger ¶
type Logger struct { Up1 UplevelLogger // contains filtered or unexported fields }
Logger represents an individual logger with logging level permissions.
func NewLogger ¶
NewLogger creates a logger for use of writting logs within the scope of a configured logging level.
func (*Logger) Complete ¶
Complete is used for the exit of a function. Min logLevel required for logging: LevelTrace(4)
func (*Logger) CompleteErr ¶
CompleteErr is used to write an error with complete into the trace. Min logLevel required for logging: LevelError(1)
func (*Logger) CompleteErrf ¶
func (l *Logger) CompleteErrf(err error, context interface{}, function string, format string, a ...interface{})
CompleteErrf is used to write an error with complete into the trace with a formatted message. Min logLevel required for logging: LevelError(1)
func (*Logger) Completef ¶
Completef is used for the exit of a function with a formatted message. Min logLevel required for logging: LevelTrace(4)
func (*Logger) DataBlock ¶
DataBlock is used to write a block of data into the trace. Min logLevel required for logging: LevelOutput(3)
func (*Logger) DataKV ¶
DataKV is used to write a key/value pair into the trace. Min logLevel required for logging: LevelOutput(3)
func (*Logger) DataString ¶
DataString is used to write a string with CRLF each on their own line. Min logLevel required for logging: LevelOutput(3)
func (*Logger) DataTrace ¶
DataTrace is used to write a block of data from an io.Stringer respecting each line. Min logLevel required for logging: LevelOutput(3)
func (*Logger) Err ¶
Err is used to write an error into the trace. Min logLevel required for logging: LevelError(1)
func (*Logger) ErrFatal ¶
ErrFatal is used to write an error into the trace then terminate the program. Min logLevel required for logging: LevelError(1)
func (*Logger) ErrFatalf ¶
func (l *Logger) ErrFatalf(err error, context interface{}, function string, format string, a ...interface{})
ErrFatalf is used to write an error into the trace with a formatted message then terminate the program. Min logLevel required for logging: LevelError(1)
func (*Logger) ErrPanic ¶
ErrPanic is used to write an error into the trace then panic the program. Min logLevel required for logging: LevelError(1)
func (*Logger) ErrPanicf ¶
func (l *Logger) ErrPanicf(err error, context interface{}, function string, format string, a ...interface{})
ErrPanicf is used to write an error into the trace with a formatted message then panic the program. Min logLevel required for logging: LevelError(1)
func (*Logger) Errf ¶
func (l *Logger) Errf(err error, context interface{}, function string, format string, a ...interface{})
Errf is used to write an error into the trace with a formatted message. Min logLevel required for logging: LevelError(1)
func (*Logger) Queryf ¶
Queryf is used to write a query into the trace with a formatted message. Min logLevel required for logging: LevelTrace(4)
func (*Logger) Start ¶
Start is used for the entry into a function. Min logLevel required for logging: LevelTrace(4)
func (*Logger) Startf ¶
Startf is used for the entry into a function with a formatted message. Min logLevel required for logging: LevelTrace(4)
type SafeBuffer ¶
type SafeBuffer struct {
// contains filtered or unexported fields
}
SafeBuffer is an object that can be used to safely use bytes.Buffer. It uses a mutex to protect the buffer and wraps few methods that can be used during various test cases.
func (*SafeBuffer) Reset ¶
func (b *SafeBuffer) Reset()
Reset is a wrapper to safely call bytes.Buffer's Reset.
func (*SafeBuffer) String ¶
func (b *SafeBuffer) String() string
String is a wrapper to safely call bytes.Buffer's String.
type SplunkPair ¶
type SplunkPair struct { Key string Value interface{} }
SplunkPair represents the key/value pairs to be logged in splunk.
type SplunkValue ¶
type SplunkValue []interface{}
SplunkValue represents a slice of values to be logged in splunk.
func (SplunkValue) String ¶
func (sl SplunkValue) String() string
String is a stringer function for the SplunkValue (which is a slice of SplunkPairs). Its main function is to encompass a list (empty, single member, or multiple members) within square brackets with ", " as a separator.
type Uplevel ¶
type Uplevel int
Uplevel controls the stack frame level for file name, line number and function name. It can be used to embed logging calls in helper functions that report the file name, line number and function name of the routine that calls the helper.
var Up1 Uplevel = 1
Up1 is short for Uplevel(1).
func (Uplevel) CompleteErr ¶
CompleteErr is used to write an error with complete into the trace.
func (Uplevel) CompleteErrf ¶
func (lvl Uplevel) CompleteErrf(err error, context interface{}, function string, format string, a ...interface{})
CompleteErrf is used to write an error with complete into the trace with a formatted message.
func (Uplevel) DataString ¶
DataString is used to write a string with CRLF each on their own line.
func (Uplevel) DataTrace ¶
DataTrace is used to write a block of data from an io.Stringer respecting each line.
func (Uplevel) ErrFatal ¶
ErrFatal is used to write an error into the trace then terminate the program.
func (Uplevel) ErrFatalf ¶
func (lvl Uplevel) ErrFatalf(err error, context interface{}, function string, format string, a ...interface{})
ErrFatalf is used to write an error into the trace with a formatted message then terminate the program.
func (Uplevel) ErrPanicf ¶
func (lvl Uplevel) ErrPanicf(err error, context interface{}, function string, format string, a ...interface{})
ErrPanicf is used to write an error into the trace with a formatted message then panic the program.
func (Uplevel) Errf ¶
func (lvl Uplevel) Errf(err error, context interface{}, function string, format string, a ...interface{})
Errf is used to write an error into the trace with a formatted message.
func (Uplevel) Splunk ¶
func (lvl Uplevel) Splunk(m ...SplunkPair)
Splunk is used to write a log message in a splunk-able format.
func (Uplevel) Start ¶
Start is used for the entry into a function.
Example ¶
ExampleUplevel_Start provides an example of using the level up functionality.
package main import ( "fmt" "github.com/Comcast/go-log/log" ) func main() { // The following code would generate a log message with line // number 13 (the line number where handleErr is called) and function name // "example.someFunc". // // 1: package example // 2: // 3: func handleErr(err error, context interface{}, function string) { // 4: ... // 5: // log with caller's file, line, and function name. // 6: log.Up1.Err(err, context, function) // 7: ... // 8: } // 9: // 10: func someFunc() { // 11: ... // 12: if err := doSomething(); err != nil { // 13: handleErr(err, context, "") // 14: } // 15: ... // 16: } // Init the log system using a buffer for testing. buf := new(log.SafeBuffer) log.InitTest("EXAMPLE", 10, log.DevWriter{Device: log.DevAll, Writer: buf}) { levelUp := func(context interface{}, function string) { log.Up1.Tracef(context, function, "Test") } log.Start("1234", "Basic") levelUp("1234", "Basic") log.Complete("1234", "Basic") } log.Shutdown() fmt.Println(buf.String()) }
Output: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Basic: Started: 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Basic: Trace: Test 2009/11/10 15:00:00.000000000: EXAMPLE[69910]: file.go#512: 1234: Basic: Completed:
type UplevelLogger ¶
type UplevelLogger struct {
// contains filtered or unexported fields
}
UplevelLogger controls the stack frame level for file name, line number and function name. It can be used to embed logging calls in helper functions that report the file name, line number and function name of the routine that calls the helper.
func (UplevelLogger) Complete ¶
func (lvl UplevelLogger) Complete(context interface{}, function string)
Complete is used for the exit of a function. Min logLevel required for logging: LevelTrace(4)
func (UplevelLogger) CompleteErr ¶
func (lvl UplevelLogger) CompleteErr(err error, context interface{}, function string)
CompleteErr is used to write an error with complete into the trace. Min logLevel required for logging: LevelError(1)
func (UplevelLogger) CompleteErrf ¶
func (lvl UplevelLogger) CompleteErrf(err error, context interface{}, function string, format string, a ...interface{})
CompleteErrf is used to write an error with complete into the trace with a formatted message. Min logLevel required for logging: LevelError(1)
func (UplevelLogger) Completef ¶
func (lvl UplevelLogger) Completef(context interface{}, function string, format string, a ...interface{})
Completef is used for the exit of a function with a formatted message. Min logLevel required for logging: LevelTrace(4)
func (UplevelLogger) DataBlock ¶
func (lvl UplevelLogger) DataBlock(context interface{}, function string, block interface{})
DataBlock is used to write a block of data into the trace. Min logLevel required for logging: LevelOutput(3)
func (UplevelLogger) DataKV ¶
func (lvl UplevelLogger) DataKV(context interface{}, function string, key string, value interface{})
DataKV is used to write a key/value pair into the trace. Min logLevel required for logging: LevelOutput(3)
func (UplevelLogger) DataString ¶
func (lvl UplevelLogger) DataString(context interface{}, function string, message string)
DataString is used to write a string with CRLF each on their own line. Min logLevel required for logging: LevelOutput(3)
func (UplevelLogger) DataTrace ¶
func (lvl UplevelLogger) DataTrace(context interface{}, function string, formatters ...Formatter)
DataTrace is used to write a block of data from an io.Stringer respecting each line. Min logLevel required for logging: LevelOutput(3)
func (UplevelLogger) Err ¶
func (lvl UplevelLogger) Err(err error, context interface{}, function string)
Err is used to write an error into the trace. Min logLevel required for logging: LevelError(1)
func (UplevelLogger) ErrFatal ¶
func (lvl UplevelLogger) ErrFatal(err error, context interface{}, function string)
ErrFatal is used to write an error into the trace then terminate the program. Min logLevel required for logging: LevelError(1)
func (UplevelLogger) ErrFatalf ¶
func (lvl UplevelLogger) ErrFatalf(err error, context interface{}, function string, format string, a ...interface{})
ErrFatalf is used to write an error into the trace with a formatted message then terminate the program. Min logLevel required for logging: LevelError(1)
func (UplevelLogger) ErrPanic ¶
func (lvl UplevelLogger) ErrPanic(err error, context interface{}, function string)
ErrPanic is used to write an error into the trace then panic the program. Min logLevel required for logging: LevelError(1)
func (UplevelLogger) ErrPanicf ¶
func (lvl UplevelLogger) ErrPanicf(err error, context interface{}, function string, format string, a ...interface{})
ErrPanicf is used to write an error into the trace with a formatted message then panic the program. Min logLevel required for logging: LevelError(1)
func (UplevelLogger) Errf ¶
func (lvl UplevelLogger) Errf(err error, context interface{}, function string, format string, a ...interface{})
Errf is used to write an error into the trace with a formatted message. Min logLevel required for logging: LevelError(1)
func (UplevelLogger) Queryf ¶
func (lvl UplevelLogger) Queryf(context interface{}, function string, format string, a ...interface{})
Queryf is used to write a query into the trace with a formatted message. Min logLevel required for logging: LevelTrace(4)
func (UplevelLogger) Start ¶
func (lvl UplevelLogger) Start(context interface{}, function string)
Start is used for the entry into a function. Min logLevel required for logging: LevelTrace(4)
func (UplevelLogger) Startf ¶
func (lvl UplevelLogger) Startf(context interface{}, function string, format string, a ...interface{})
Startf is used for the entry into a function with a formatted message. Min logLevel required for logging: LevelTrace(4)
func (UplevelLogger) Tracef ¶
func (lvl UplevelLogger) Tracef(context interface{}, function string, format string, a ...interface{})
Tracef is used to write information into the trace with a formatted message. Min logLevel required for logging: LevelTrace(4)
func (UplevelLogger) Warnf ¶
func (lvl UplevelLogger) Warnf(context interface{}, function string, format string, a ...interface{})
Warnf is used to write a warning into the trace with a formatted message. Min logLevel required for logging: LevelWarning(2)