goose

package module
v0.0.0-...-ee4fb49 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2023 License: MPL-2.0 Imports: 8 Imported by: 25

README

Package goose is a simple golang log package for debug purposes.

GoDoc

The idea is very simple. You create variables of type goose.Alert and set its debug level. We suggest you set these variables to level 0 when you don't want any log messages at all. Set to level 1 to log error messages. Set to level 2 or above for increasing levels of log verbosity.

The package offers 4 methods to emit debug messages:

func (d Alert) Logf(level int, format string, parms ...interface{})

Based on the log.Printf.

func (d Alert) Fatalf(level int, format string, parms ...interface{})

Same as above but stops program execution. The execution ends EVEN WHEN the log level of the message is higher the the current log level.

func (d Alert) Printf(level int, format string, parms ...interface{})

Based on fmt.Printf.

func (d Alert) Sprintf(level int, format string, parms ...interface{}) string

Based on fmt.Sprintf.

In all the above methods the level parameter determines the log level of the message. The message will be actually emited only if, when the method is called, the log level is equal or higher than the message's log level. An empty string is returned by the Sprintf method if called with a message log level higher than the current log level.

Example:

In your initialization code, you may set the log levels of debug messages:

   ...

   reptilian.Goose  = goose.Alert(1) // Only error messages will be logged

   reptext.Goose    = goose.Alert(0) // No debug messages at all

   djparser.Goose   = goose.Alert(4) // Error messages and less verbose messages (levels 2~4) will be logged

   ...

In the package to be debugged:


import (
   ...

   "github.com/luisfurquim/goose"
   ...
)

...

var Goose goose.Alert // Exported symbol needed only if you want to allow external control of the debug level

...

   // throughout your code you may log anything you want at varying levels of importance

   Goose.Logf(3, "Final Off=%d (%o)", d.Buf.Off, d.Buf.Off)

   ...

   Goose.Logf(7, "Index: %#v", d.Index)

   // Logs will be actually printed only if the first parameter (the message's log level) is lower or equal than the current log level indicated by the Goose variable. Remember to never use the zero value, like Goose.Logf(0,...), as we want to make the log level 0 to print no debug messages at all.

You may set multiple loggers if you need finer control on the verbosity

type T1 struct {

...

}


type T2 struct {

...

}

var GooseT1 goose.Alert
var GooseT2 goose.Alert

   ...

   GooseT1 = goose.Alert(1) // Only error messages will be logged
   GooseT2 = goose.Alert(3) // More verbosity...

   ...

   GooseT1.Logf(2, "Final Off=%d (%o)", d.Buf.Off, d.Buf.Off) // not printed

   ...

   GooseT2.Logf(2, "Index: %#v", d.Index) // printed

You may add/remove source code reference by enabling/disabling trace. With trace enabled Goose automatically adds source code reference in the format {package}[source filename]<function/method>(line number).

Check it with the following code:


   Goose = goose.Alert(1)
   Goose.Logf(1,"no trace")
   goose.TraceOn()
   Goose.Logf(1,"trace")
   goose.TraceOff()
   Goose.Logf(1,"no trace")

Outputs:


   2015/07/28 09:42:30 no trace
   2015/07/28 09:42:30 {main}[teste-goose.go]<main>(14): trace
   2015/07/28 09:42:30 no trace

You may redirect the output to the syslogger just calling UseSyslogNet:


   goose.UseSyslogNet("tcp", "myloghost.mydomain:514", syslog.LOG_ERR|syslog.LOG_LOCAL7)

Documentation

Overview

Package goose is a simple golang log package for debug purposes.

The idea is very simple. You create variables of type goose.Alert and set its debug level. We suggest you set these variables to level 0 when you don't want any log messages at all. Set to level 1 to log error messages. Set to level 2 or above for increasing levels of log verbosity.

In all the above methods the level parameter determines the log level of the message. The message will be actually emited only if, when the method is called, the log level is equal or higher than the message's log level. An empty string is returned by the Sprintf method if called with a message log level higher than the current log level.

Example

In your initialization code, you may set the log levels of debug messages:

...

reptilian.Goose  = goose.Alert(1) // Only error messages will be logged

reptext.Goose    = goose.Alert(0) // No debug messages at all

djparser.Goose   = goose.Alert(4) // Error messages and less verbose messages (levels 2~4) will be logged

...

In the package to be debugged:

import (
   ...

   "github.com/luisfurquim/goose"
   ...
)

...

var Goose goose.Alert // Exported symbol needed only if you want to allow external control of the debug level

...

   // throughout your code you may log anything you want at varying levels of importance

   Goose.Logf(3, "Final Off=%d (%o)", d.Buf.Off, d.Buf.Off)

   ...

   Goose.Logf(7, "Index: %#v", d.Index)

   // Logs will be actually printed only if the first parameter (the message's log level) is lower or equal than the current log level indicated by the Goose variable. Remember to never use the zero value, like Goose.Logf(0,...), as we want to make the log level 0 to print no debug messages at all.

You may set multiple loggers if you need finer control on the verbosity:

type T1 struct {

...

}

type T2 struct {

...

}

var GooseT1 goose.Alert
var GooseT2 goose.Alert

   ...

   GooseT1 = goose.Alert(1) // Only error messages will be logged
   GooseT2 = goose.Alert(3) // More verbosity...

   ...

   GooseT1.Logf(2, "Final Off=%d (%o)", d.Buf.Off, d.Buf.Off) // not printed

   ...

   GooseT2.Logf(2, "Index: %#v", d.Index) // printed

Tracing

You may add/remove source code reference by enabling/disabling trace. With trace enabled Goose automatically adds source code reference in the format {package}[source filename]&lt;function/method&gt;(line number).

Check it with the following code:

Goose = goose.Alert(1)
Goose.Logf(1,"no trace")
goose.TraceOn()
Goose.Logf(1,"trace")
goose.TraceOff()
Goose.Logf(1,"no trace")

// Outputs:
// 2015/07/28 09:42:30 no trace
// 2015/07/28 09:42:30 {main}[teste-goose.go]<main>(14): trace
// 2015/07/28 09:42:30 no trace

Redirecting output

You may redirect the output to the syslogger just calling UseSyslogNet:

goose.UseSyslogNet("tcp", "myloghost.mydomain:514", syslog.LOG_ERR|syslog.LOG_LOCAL7)

Index

Constants

This section is empty.

Variables

View Source
var GooseValType reflect.Type = reflect.TypeOf(Alert(0))

Functions

func TraceOff

func TraceOff()

TraceOff disables the inclusion of the package name, source file name, method/function caller and source line number in the log messages. This is the default state of logging.

func TraceOn

func TraceOn()

TraceOn enables the inclusion of the package name, source file name, method/function caller and source line number in the log messages. As stated in https://golang.org/pkg/runtime/#Func.FileLine about the line numbering, "The result will not be accurate if pc is not a program counter within f".

func UseSyslogNet

func UseSyslogNet(proto string, srv string, priority syslog.Priority) error

UseSyslogNet redirects the log output from os.Stderr to the system logger connecting to it via network.

Types

type Alert

type Alert uint8

Alert is the basic type that implements the goose log object

func (Alert) DeepFatalf

func (d Alert) DeepFatalf(stacklevel, level int, format string, parms ...interface{})

Fatalf behaves as Logf but stops program execution. The execution ends EVEN WHEN the log level of the message is higher the the current log level.

func (Alert) DeepLogf

func (d Alert) DeepLogf(stacklevel, level int, format string, parms ...interface{})

Logf emits the messages based on the log.Printf

func (Alert) DeepPrintf

func (d Alert) DeepPrintf(stacklevel, level int, format string, parms ...interface{})

Logf emits the messages based on fmt.Printf

func (Alert) DeepSprintf

func (d Alert) DeepSprintf(stacklevel, level int, format string, parms ...interface{}) string

Sprintf returns the messages as a string value

func (Alert) Fatalf

func (d Alert) Fatalf(level int, format string, parms ...interface{})

Fatalf behaves as Logf but stops program execution. The execution ends EVEN WHEN the log level of the message is higher the the current log level.

func (Alert) Logf

func (d Alert) Logf(level int, format string, parms ...interface{})

Logf emits the messages based on the log.Printf

func (Alert) Printf

func (d Alert) Printf(level int, format string, parms ...interface{})

Logf emits the messages based on fmt.Printf

func (*Alert) Set

func (d *Alert) Set(level interface{})

Set accepts either integer or string values to initialize the goose Alert level

func (Alert) Sprintf

func (d Alert) Sprintf(level int, format string, parms ...interface{}) string

Sprintf returns the messages as a string value

type Geese

type Geese map[string]interface{}

func (Geese) Get

func (geese Geese) Get() map[string]interface{}

func (Geese) Set

func (geese Geese) Set(level interface{})

func (Geese) UnmarshalJSON

func (geese Geese) UnmarshalJSON(data []byte) error

type SyslogGoose

type SyslogGoose struct {
	W *syslog.Writer
}

SyslogGoose is a wrapper type around *syslog.Writer to ensure a syslogger that satisfies the io.Writer inrterface

func (SyslogGoose) Write

func (ng SyslogGoose) Write(b []byte) (int, error)

Write is the method that satisfies the io.Writer inrterface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL