Documentation
¶
Overview ¶
graylogger was made to provide easy-to-analyze log messages. For sending GELF messages it uses the: https://github.com/Devatoria/go-graylog package.
Index ¶
- Constants
- type Functions
- type GrayLogger
- func (g *GrayLogger) CaptureOutput(fileName string) *GrayLogger
- func (g *GrayLogger) Debug(keysAndValues ...interface{})
- func (g *GrayLogger) DiscardOutput()
- func (g *GrayLogger) Error(keysAndValues ...interface{})
- func (g *GrayLogger) Fatal(err error)
- func (g *GrayLogger) GetInit() Init
- func (g *GrayLogger) GetLogLevel() (int, string)
- func (g *GrayLogger) GetOutput() string
- func (g *GrayLogger) Info(keysAndValues ...interface{})
- func (g *GrayLogger) IsAllowedOutput() bool
- func (g *GrayLogger) LogErrorIfErr(err error)
- func (g *GrayLogger) LogWarningIfErr(err error)
- func (g *GrayLogger) PrintOutput()
- func (g *GrayLogger) ResetLogger() *GrayLogger
- func (g *GrayLogger) ReturnWithError(keysAndValues ...interface{}) error
- func (g *GrayLogger) SaveOutput()
- func (g *GrayLogger) SendGELF(level int, keysAndValues ...interface{})
- func (g *GrayLogger) Warning(keysAndValues ...interface{})
- type GraylogExtraFields
- type Init
- type LogLevel
- type TrackInfo
- type Transport
Examples ¶
- GrayLogger.Debug
- GrayLogger.DiscardOutput
- GrayLogger.Error
- GrayLogger.Fatal
- GrayLogger.GetInit
- GrayLogger.GetLogLevel
- GrayLogger.GetOutput
- GrayLogger.Info
- GrayLogger.IsAllowedOutput
- GrayLogger.LogErrorIfErr
- GrayLogger.LogWarningIfErr
- GrayLogger.PrintOutput
- GrayLogger.ResetLogger
- GrayLogger.ReturnWithError
- GrayLogger.SaveOutput
- GrayLogger.SendGELF
- GrayLogger.Warning
- New
- Tracking
Constants ¶
const ( // TransportTCP is connection-oriented, and a connection between client // and server is established (passive open) before data can be sent. TransportTCP Transport = "tcp" // TransportUDP is suitable for purposes where error checking // and correction are either not necessary or are performed in the application TransportUDP Transport = "udp" // LevelDebug logs everything LevelDebug LogLevel = "debug" // LevelInfo logs Info, Warnings and Errors LevelInfo LogLevel = "info" // LevelWarning logs Warning and Errors LevelWarning LogLevel = "warning" // LevelError logs just Errors LevelError LogLevel = "error" // LevelFatal logs just Fatal errors and aborts the application LevelFatal LogLevel = "fatal" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Functions ¶
type Functions struct {
Debug *log.Logger // Designates fine-grained informational events that are most useful to debug an application.
Info *log.Logger // Designates informational messages that highlight the progress of the application at coarse-grained level.
Warning *log.Logger // Designates potentially harmful situations.
Error *log.Logger // Designates error events that might still allow the application to continue running.
Fatal *log.Logger // Designates very severe error events that will presumably lead the application to abort.
}
Functions provide a different kind of logging writers which controlled by log level.
type GrayLogger ¶
type GrayLogger struct {
// contains filtered or unexported fields
}
GrayLogger holds the needed data to use the functions of this package.
- initData -> the data with which the package was initialized
- functions -> logger functions: Debug, Info, Warning, Error, Fatal
- level -> log level converted to integer
- fileName -> set by CaptureOutput() function, provides the filename where output can be saved
- fileOpen -> set by CaptureOutput() function, holds *os.File
- graylog -> set by connect() function, it represents an established graylog connection
func New ¶
func New(init Init) *GrayLogger
New configures the logging writers.
Example ¶
package main
import (
"fmt"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: true,
})
fmt.Println(g.GetInit())
}
Output: { 0 100ms test debug true}
func (*GrayLogger) CaptureOutput ¶
func (g *GrayLogger) CaptureOutput(fileName string) *GrayLogger
CaptureOutput will redirect logger output to a given fileName.
func (*GrayLogger) Debug ¶
func (g *GrayLogger) Debug(keysAndValues ...interface{})
Debug writes Info to stdOut and sends GELF message to Graylog.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
g.CaptureOutput("test.out")
g.Debug("test", graylogger.LevelDebug)
g.SaveOutput()
output := g.GetOutput()
fmt.Println(strings.Contains(output, "[DEBUG]"))
fmt.Println(strings.Contains(output, "file: output_example_test.go"))
fmt.Println(strings.Contains(output, "function: graylogger_test.ExampleGrayLogger_Debug"))
fmt.Println(strings.Contains(output, "[test :: debug]"))
}
Output: true true true true
func (*GrayLogger) DiscardOutput ¶
func (g *GrayLogger) DiscardOutput()
DiscardOutput discards all level outputs and prevents sending messages to Graylog as well.
Example ¶
package main
import (
"fmt"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: true,
})
g.DiscardOutput()
fmt.Println(g.IsAllowedOutput())
}
Output: false
func (*GrayLogger) Error ¶
func (g *GrayLogger) Error(keysAndValues ...interface{})
Error writes Error to stdOut and sends GELF message to Graylog.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
g.CaptureOutput("test.out")
g.Error("test", graylogger.LevelError)
g.SaveOutput()
output := g.GetOutput()
fmt.Println(strings.Contains(output, "[ERROR]"))
fmt.Println(strings.Contains(output, "file: output_example_test.go"))
fmt.Println(strings.Contains(output, "function: graylogger_test.ExampleGrayLogger_Error"))
fmt.Println(strings.Contains(output, "[test :: error]"))
}
Output: true true true true
func (*GrayLogger) Fatal ¶
func (g *GrayLogger) Fatal(err error)
Fatal writes Error to stdOut and exit with exit code 1, if err doesn't nil. It also sends GELF message to Graylog, if it possible.
Example ¶
package main
import (
"fmt"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
err := fmt.Errorf("test :: %s", graylogger.LevelError)
g.Fatal(err)
// [FATAL] 2020/02/03 14:43:37 [file: example.go line: 17 function: main.main] [main.main :: test :: error]
// exit status 1
}
Output:
func (*GrayLogger) GetInit ¶
func (g *GrayLogger) GetInit() Init
GetInit returns with the initial logger data.
Example ¶
package main
import (
"fmt"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
init := g.GetInit()
fmt.Println(init.LogEnv)
fmt.Println(init.LogLevel)
fmt.Println(init.LogColor)
}
Output: test debug false
func (*GrayLogger) GetLogLevel ¶
func (g *GrayLogger) GetLogLevel() (int, string)
GetLogLevel returns with the set log level (int, string).
Example ¶
package main
import (
"fmt"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: true,
})
levelNum, levelString := g.GetLogLevel()
fmt.Println(levelNum)
fmt.Println(levelString)
}
Output: 7 debug
func (*GrayLogger) GetOutput ¶
func (g *GrayLogger) GetOutput() string
GetOutput reads the file content what we set in the CaptureOutput() function.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
g.CaptureOutput("test.out")
g.Debug("test", "get_output")
g.SaveOutput()
output := g.GetOutput()
fmt.Println(strings.Contains(output, "[DEBUG]"))
fmt.Println(strings.Contains(output, "file: output_example_test.go"))
fmt.Println(strings.Contains(output, "function: graylogger_test.ExampleGrayLogger_GetOutput"))
fmt.Println(strings.Contains(output, "[test :: get_output]"))
}
Output: true true true true
func (*GrayLogger) Info ¶
func (g *GrayLogger) Info(keysAndValues ...interface{})
Info writes Info to stdOut and sends GELF message to Graylog.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
g.CaptureOutput("test.out")
g.Info("test", graylogger.LevelInfo)
g.SaveOutput()
output := g.GetOutput()
fmt.Println(strings.Contains(output, "[INFO]"))
fmt.Println(strings.Contains(output, "file: output_example_test.go"))
fmt.Println(strings.Contains(output, "function: graylogger_test.ExampleGrayLogger_Info"))
fmt.Println(strings.Contains(output, "[test :: info]"))
}
Output: true true true true
func (*GrayLogger) IsAllowedOutput ¶
func (g *GrayLogger) IsAllowedOutput() bool
IsAllowedOutput tells that StdOut is allowed or discarded on all log levels.
Example ¶
package main
import (
"fmt"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: true,
})
fmt.Println(g.IsAllowedOutput())
}
Output: true
func (*GrayLogger) LogErrorIfErr ¶
func (g *GrayLogger) LogErrorIfErr(err error)
LogErrorIfErr only writes Error to stdOut, if err doesn't nil. It also sends GELF message to Graylog, if it possible.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
g.CaptureOutput("test.out")
err := fmt.Errorf("test :: %s", graylogger.LevelError)
g.LogErrorIfErr(err)
g.SaveOutput()
output := g.GetOutput()
fmt.Println(strings.Contains(output, "[ERROR]"))
fmt.Println(strings.Contains(output, "file: output_example_test.go"))
fmt.Println(strings.Contains(output, "function: graylogger_test.ExampleGrayLogger_LogErrorIfErr"))
fmt.Println(strings.Contains(output, "[graylogger_test.ExampleGrayLogger_LogErrorIfErr :: test :: error]"))
}
Output: true true true true
func (*GrayLogger) LogWarningIfErr ¶
func (g *GrayLogger) LogWarningIfErr(err error)
LogWarningIfErr only writes Warning to stdOut, if err doesn't nil. It also sends GELF message to Graylog, if it possible.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
g.CaptureOutput("test.out")
err := fmt.Errorf("test :: %s", graylogger.LevelWarning)
g.LogWarningIfErr(err)
g.SaveOutput()
output := g.GetOutput()
fmt.Println(strings.Contains(output, "[WARNING]"))
fmt.Println(strings.Contains(output, "file: output_example_test.go"))
fmt.Println(strings.Contains(output, "function: graylogger_test.ExampleGrayLogger_LogWarningIfErr"))
fmt.Println(strings.Contains(output, "[graylogger_test.ExampleGrayLogger_LogWarningIfErr :: test :: warning]"))
}
Output: true true true true
func (*GrayLogger) PrintOutput ¶
func (g *GrayLogger) PrintOutput()
PrintOutput prints out the file content what we set in the CaptureOutput() function.
Example ¶
package main
import (
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
g.CaptureOutput("test.out")
g.Debug("test", "print_output")
g.SaveOutput()
g.PrintOutput()
// [DEBUG] 2020/02/03 14:44:37 [file: example.go line: 15 function: main.main] [test :: print_output]
}
Output:
func (*GrayLogger) ResetLogger ¶
func (g *GrayLogger) ResetLogger() *GrayLogger
ResetLogger allows StdOut with the initialized log level and allows sending messages to Graylog as well.
Example ¶
package main
import (
"fmt"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: true,
})
g.DiscardOutput()
fmt.Println(g.IsAllowedOutput())
g = g.ResetLogger()
fmt.Println(g.IsAllowedOutput())
}
Output: false true
func (*GrayLogger) ReturnWithError ¶
func (g *GrayLogger) ReturnWithError(keysAndValues ...interface{}) error
ReturnWithError writes Error to stdOut and returns with that error message at the same time. It also sends GELF message to Graylog, if it possible.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
g.CaptureOutput("test.out")
ret := g.ReturnWithError("test", graylogger.LevelError)
g.SaveOutput()
output := g.GetOutput()
fmt.Println(strings.Contains(output, "[ERROR]"))
fmt.Println(strings.Contains(output, "file: output_example_test.go"))
fmt.Println(strings.Contains(output, "function: graylogger_test.ExampleGrayLogger_ReturnWithError"))
fmt.Println(ret)
}
Output: true true true test :: error
func (*GrayLogger) SaveOutput ¶
func (g *GrayLogger) SaveOutput()
SaveOutput will close the file, what we set in CaptureOutput() function and re-set the output of all logger functions .
Example ¶
package main
import (
"fmt"
"strings"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
g.CaptureOutput("test.out")
g.Debug("test", "save_output")
g.SaveOutput()
output := g.GetOutput()
fmt.Println(strings.Contains(output, "[DEBUG]"))
fmt.Println(strings.Contains(output, "file: output_example_test.go"))
fmt.Println(strings.Contains(output, "function: graylogger_test.ExampleGrayLogger_SaveOutput"))
fmt.Println(strings.Contains(output, "[test :: save_output]"))
}
Output: true true true true
func (*GrayLogger) SendGELF ¶
func (g *GrayLogger) SendGELF(level int, keysAndValues ...interface{})
SendGELF sends GELF messages into Graylog instance. If the Graylog host is unreachable, it writes an error message to stdOut.
Example ¶
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net"
"time"
"github.com/takattila/graylogger"
)
func main() {
// UDP server initialization ...
udpServer := func(port int, response chan string) {
conn, err := net.ListenUDP("udp", &net.UDPAddr{
Port: port,
IP: net.ParseIP("0.0.0.0"),
})
if err != nil {
log.Fatalf("net.ListenUDP: %s\n", err)
}
defer func() {
_ = conn.Close()
}()
message := make([]byte, 262144)
_, _, err = conn.ReadFromUDP(message[:])
if err != nil {
log.Fatalf("conn.ReadFromUDP: %s\n", err)
}
response <- string(message)
}
// Setup Init structure ...
init := graylogger.Init{
GraylogHost: "127.0.0.1",
GraylogPort: 12201,
GraylogProvider: "TestService",
GraylogProtocol: graylogger.TransportUDP,
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
}
// Starting UDP server ...
response := make(chan string)
go udpServer(init.GraylogPort, response)
time.Sleep(100 * time.Millisecond)
// GrayLogger initialization ...
g := graylogger.New(init)
g.CaptureOutput("test.out")
g.Info("test", "gelf_message")
g.SaveOutput()
// Removing NUL characters from bytes
responseBytes := bytes.Trim([]byte(<-response), "\x00")
// Parsing JSON response ...
obj := map[string]interface{}{}
err := json.Unmarshal(responseBytes, &obj)
if err != nil {
log.Fatalf("json.Unmarshal: %v\n", err)
}
fmt.Println(obj["_log_env"])
fmt.Println(obj["_log_key"])
fmt.Println(obj["_log_level"])
fmt.Println(obj["_log_value"])
fmt.Println(obj["_track_file"])
fmt.Println(obj["_track_function"])
fmt.Println(obj["full_message"])
fmt.Println(obj["short_message"])
fmt.Println(obj["host"])
fmt.Println(obj["version"])
fmt.Println(obj["level"])
}
Output: test test info gelf_message graylog_example_test.go graylogger_test.ExampleGrayLogger_SendGELF test :: gelf_message test :: gelf_message TestService 1.1 6
func (*GrayLogger) Warning ¶
func (g *GrayLogger) Warning(keysAndValues ...interface{})
Warning writes Warning to stdOut and sends GELF message to Graylog.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/takattila/graylogger"
)
func main() {
g := graylogger.New(graylogger.Init{
LogEnv: "test",
LogLevel: graylogger.LevelDebug,
LogColor: false,
})
g.CaptureOutput("test.out")
g.Warning("test", graylogger.LevelWarning)
g.SaveOutput()
output := g.GetOutput()
fmt.Println(strings.Contains(output, "[WARNING]"))
fmt.Println(strings.Contains(output, "file: output_example_test.go"))
fmt.Println(strings.Contains(output, "function: graylogger_test.ExampleGrayLogger_Warning"))
fmt.Println(strings.Contains(output, "[test :: warning]"))
}
Output: true true true true
type GraylogExtraFields ¶
type GraylogExtraFields struct {
Env string `json:"log_env"`
Level string `json:"log_level"`
Key string `json:"log_key"`
Value string `json:"log_value"`
Line string `json:"track_line"`
File string `json:"track_file"`
Function string `json:"track_function"`
}
GraylogExtraFields represents the extra GELF data which will be sent into Graylog instance.
type Init ¶
type Init struct {
GraylogHost string // Host name of the Graylog server
GraylogPort int // Port number of the Graylog server
GraylogProvider string // The Name of the service which generates the log messages or sends logs into Graylog
GraylogProtocol Transport // The name of the transport protocol: the way we send GELF messages (TransportTCP or TransportUDP)
GraylogTimeout time.Duration // Optional, it declares the maximum amount of time a dial will wait for a connection to complete.
LogEnv string // Environment of the service: dev / test / prod
LogLevel LogLevel // It can be: debug, info, warning, error
LogColor bool // Turn on/off colored log output. Color output can be useful during development, but it is recommended to turn it off in production environment.
}
Init initializes the logger instance
type TrackInfo ¶
TrackInfo holds debug information about the caller's:
- File name
- Line number
- Function name
func Tracking ¶
Tracking provides debug information about function invocations on the calling goroutine's stack:
- File (where Tracking was called)
- Line (where function was called)
- Function (name of the function)
Example ¶
package main
import (
"fmt"
"strconv"
"github.com/takattila/graylogger"
)
func main() {
track := graylogger.Tracking(1)
fmt.Println(track.File)
num, _ := strconv.Atoi(track.Line)
fmt.Printf("%T\n", num)
fmt.Println(track.Function)
}
Output: output_example_test.go int graylogger_test.ExampleTracking

