log

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2022 License: MIT Imports: 8 Imported by: 7

README

log Build Status Go Reference

Go logging library for GCP App Engine, Cloud Run, Cloud Functions:

  • Encodes the severities so that coloring works in the Google web-based Logs Explorer.
  • Zero dependencies, it is a stdlib-only module. Nul, zilch, nada!
  • Safe for concurrent logging.
  • Backward compatible with older Go versions as far as 1.9.4.

Screenshot

Coloring on GCP

Examples

package main

import "github.com/apsystole/log"

func main() {
    log.Print("my message")
}

More details to be found in the documentation on pkg.go.dev.

Documentation

Overview

Package log implements structured logging for Google App Engine, Cloud Run and Cloud Functions. The API is compatible with the standard library "log" module.

All the severities conform to the Google Cloud Logging API v2 as described in https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#logseverity. These severity levels are: DEBUG, INFO, NOTICE, WARNING, ERROR, CRITICAL, ALERT, EMERGENCY.

The ERROR, CRITICAL, ALERT, EMERGENCY logs are written to the standard error stream, while the remaining logs are written to the standard output.

Example

Example shows typical severity recommendations.

package main

import "github.com/apsystole/log"

// Example shows typical severity recommendations.
func main() {
	log.Debug("the lowest level")
	log.Print("phase 2 processing ended") // or log.Info()
	log.Notice("shutting down normally...")
	log.Warning("difficulties, retrying shutdown...")
}

// Errors are used sparingly to draw attention of a human, sooner or later.
// nolint: unused
func Errors() {
	log.Error("a minor problem that a real human should act on")
	log.Critical("a major problem")
	log.Fatal("a major problem - aborting")
	log.Panic("a major problem - dumping stacktraces and aborting")
	log.Alert("waky waky dear on-duty person, eggs and baky")
	log.Emergency("core functionality is down and needs a human to rescue it")
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var ProjectID = os.Getenv("GOOGLE_CLOUD_PROJECT")

ProjectID should be set to the Google Cloud project ID to properly correlate the message traces to HTTP requests, if you use ForRequest. The initial value is taken from the environment variable GOOGLE_CLOUD_PROJECT.

Functions

func Alert

func Alert(v ...interface{})

Alert logs when a person must take an action immediately. Arguments are handled in the manner of fmt.Print.

func Alertf

func Alertf(format string, v ...interface{})

Alertf logs when a person must take an action immediately. Arguments are handled in the manner of fmt.Printf.

func Alertj

func Alertj(msg string, v interface{})

Alertj logs when a person must take an action immediately. Argument v becomes the jsonPayload field of the log entry.

func Alertln

func Alertln(v ...interface{})

Alertln logs when a person must take an action immediately. Arguments are handled in the manner of fmt.Println.

func Critical

func Critical(v ...interface{})

Critical logs events that cause more severe problems or outages. Arguments are handled in the manner of fmt.Print.

func Criticalf

func Criticalf(format string, v ...interface{})

Criticalf logs events that cause more severe problems or outages. Arguments are handled in the manner of fmt.Printf.

func Criticalj

func Criticalj(msg string, v interface{})

Criticalj logs events that cause more severe problems or outages. Argument v becomes the jsonPayload field of the log entry.

func Criticalln

func Criticalln(v ...interface{})

Criticalln logs events that cause more severe problems or outages. Arguments are handled in the manner of fmt.Println.

func Debug

func Debug(v ...interface{})

Debug logs detailed information that could mainly be used to catch unforeseen problems. Arguments are handled in the manner of fmt.Print.

func Debugf

func Debugf(format string, v ...interface{})

Debugf logs detailed information that could mainly be used to catch unforeseen problems. Arguments are handled in the manner of fmt.Printf.

func Debugj

func Debugj(msg string, v interface{})

Debugj logs detailed information that could mainly be used to catch unforeseen problems. Argument v becomes jsonPayload field in the log entry.

func Debugln

func Debugln(v ...interface{})

Debugln logs detailed information that could mainly be used to catch unforeseen problems. Arguments are handled in the manner of fmt.Println.

func Emergency

func Emergency(v ...interface{})

Emergency logs when one or more systems are unusable. Arguments are handled in the manner of fmt.Print.

func Emergencyf

func Emergencyf(format string, v ...interface{})

Emergencyf logs when one or more systems are unusable. Arguments are handled in the manner of fmt.Printf.

func Emergencyj

func Emergencyj(msg string, v interface{})

Emergencyj logs when one or more systems are unusable. Argument v becomes the jsonPayload field of the log entry.

func Emergencyln

func Emergencyln(v ...interface{})

Emergencyln logs when one or more systems are unusable. Arguments are handled in the manner of fmt.Println.

func Error

func Error(v ...interface{})

Error logs events likely to cause problems. Arguments are handled in the manner of fmt.Print.

func Errorf

func Errorf(format string, v ...interface{})

Errorf logs events likely to cause problems. Arguments are handled in the manner of fmt.Printf.

func Errorj

func Errorj(msg string, v interface{})

Errorj logs events likely to cause problems. Argument v becomes the jsonPayload field of the log entry.

func Errorln

func Errorln(v ...interface{})

Errorln logs events likely to cause problems. Arguments are handled in the manner of fmt.Println.

func Fatal

func Fatal(v ...interface{})

Fatal is equivalent to a call to Critical() followed by a call to os.Exit(1).

func Fatalf

func Fatalf(format string, v ...interface{})

Fatalf is equivalent to a call to Criticalf() followed by a call to os.Exit(1).

func Fatalj

func Fatalj(msg string, v interface{})

Fatalj is equivalent to a call to Criticalj() followed by a call to os.Exit(1).

func Fatalln

func Fatalln(v ...interface{})

Fatalln is equivalent to a call to Criticalln() followed by a call to os.Exit(1).

func Info

func Info(v ...interface{})

Info logs routine information, such as ongoing status or performance. Arguments are handled in the manner of fmt.Print.

func Infof

func Infof(format string, v ...interface{})

Infof logs routine information, such as ongoing status or performance. Arguments are handled in the manner of fmt.Printf.

func Infoj

func Infoj(msg string, v interface{})

Infoj logs routine information, such as ongoing status or performance. Argument v becomes the jsonPayload field of the log entry.

func Infoln

func Infoln(v ...interface{})

Infoln logs routine information, such as ongoing status or performance. Arguments are handled in the manner of fmt.Println.

func Notice

func Notice(v ...interface{})

Notice logs normal but significant events, such as start up, shut down, or configuration. Arguments are handled in the manner of fmt.Print.

Example
package main

import (
	"github.com/apsystole/log"
)

func main() {
	log.Notice("hello", 1, "!")
	log.Noticeln("hello", 2, "!")
}
Output:

{"message":"hello1!","severity":"NOTICE"}
{"message":"hello 2 !\n","severity":"NOTICE"}

func Noticef

func Noticef(format string, v ...interface{})

Noticef logs normal but significant events, such as start up, shut down, or configuration. Arguments are handled in the manner of fmt.Printf.

Example
package main

import (
	"io"

	"github.com/apsystole/log"
)

func main() {
	err := io.EOF
	name := "my blog.txt"
	log.Noticef("while reading file %q ignoring: %v", name, err)
}
Output:

{"message":"while reading file \"my blog.txt\" ignoring: EOF","severity":"NOTICE"}

func Noticej

func Noticej(msg string, v interface{})

Noticej logs normal but significant events, such as start up, shut down, or configuration. Argument v becomes the jsonPayload field of the log entry.

Example
package main

import (
	"github.com/apsystole/log"
)

func main() {
	obj := struct {
		Seq       int
		Component string
	}{
		Seq:       42,
		Component: "app",
	}
	log.Noticej("warning", obj)
}
Output:

{"message":"warning","severity":"NOTICE","Seq":42,"Component":"app"}

func Noticeln

func Noticeln(v ...interface{})

Noticeln logs normal but significant events, such as start up, shut down, or configuration. Arguments are handled in the manner of fmt.Println.

Example
package main

import (
	"github.com/apsystole/log"
)

func main() {
	log.Notice("hello", 1, "!")
	log.Noticeln("hello", 2, "!")
}
Output:

{"message":"hello1!","severity":"NOTICE"}
{"message":"hello 2 !\n","severity":"NOTICE"}

func Panic

func Panic(v ...interface{})

Panic is equivalent to a call to Critical() followed by a call to panic().

func Panicf

func Panicf(format string, v ...interface{})

Panicf is equivalent to a call to Criticalf() followed by a call to panic().

func Panicj

func Panicj(msg string, v interface{})

Panicj is equivalent to a call to Criticalj() followed by a call to panic().

func Panicln

func Panicln(v ...interface{})

Panicln is equivalent to a call to Criticalln() followed by a call to panic().

func Print

func Print(v ...interface{})

Print logs routine information, such as ongoing status or performance, same as Info(). Arguments are handled in the manner of fmt.Print.

func Printf

func Printf(format string, v ...interface{})

Printf logs routine information, such as ongoing status or performance, same as Infof(). Arguments are handled in the manner of fmt.Printf.

func Printj

func Printj(msg string, v interface{})

Printj logs routine information, such as ongoing status or performance, same as Infoj(). Argument v becomes the jsonPayload field of the log entry.

func Println

func Println(v ...interface{})

Println logs routine information, such as ongoing status or performance, same as Infoln(). Arguments are handled in the manner of fmt.Println.

func Warning

func Warning(v ...interface{})

Warning logs events that might cause problems. Arguments are handled in the manner of fmt.Print.

func Warningf

func Warningf(format string, v ...interface{})

Warningf logs events that might cause problems. Arguments are handled in the manner of fmt.Printf.

func Warningj

func Warningj(msg string, v interface{})

Warningj logs events that might cause problems. Argument v becomes the jsonPayload field of the log entry.

func Warningln

func Warningln(v ...interface{})

Warningln logs events that might cause problems. Arguments are handled in the manner of fmt.Println.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func ForRequest

func ForRequest(request *http.Request) *Logger

ForRequest creates a new Logger. All the messages logged through it will trace back to the HTTP request, based on its header "X-Cloud-Trace-Context" combined with the package var ProjectID.

Setting package var ProjectID to empty disables such tracing altogether.

func New added in v0.1.1

func New(w io.Writer, dummy2 string, dummy3 int) *Logger

New is for interface-level compatibility with standard library's "log" package. It creates a new Logger, which streams all its messages to w. Remaining arguments are ignored.

The ForRequest() constructor is more useful.

func (*Logger) Alert

func (l *Logger) Alert(v ...interface{})

Alert logs when a person must take an action immediately. Arguments are handled in the manner of fmt.Print.

func (*Logger) Alertf

func (l *Logger) Alertf(format string, v ...interface{})

Alertf logs when a person must take an action immediately. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Alertj

func (l *Logger) Alertj(msg string, v interface{})

Alertj logs when a person must take an action immediately. Argument v becomes the jsonPayload field of the log entry.

func (*Logger) Alertln

func (l *Logger) Alertln(v ...interface{})

Alertln logs when a person must take an action immediately. Arguments are handled in the manner of fmt.Println.

func (*Logger) Critical

func (l *Logger) Critical(v ...interface{})

Critical logs events that cause more severe problems or outages. Arguments are handled in the manner of fmt.Print.

func (*Logger) Criticalf

func (l *Logger) Criticalf(format string, v ...interface{})

Criticalf logs events that cause more severe problems or outages. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Criticalj

func (l *Logger) Criticalj(msg string, v interface{})

Criticalj logs events that cause more severe problems or outages. Argument v becomes the jsonPayload field of the log entry.

func (*Logger) Criticalln

func (l *Logger) Criticalln(v ...interface{})

Criticalln logs events that cause more severe problems or outages. Arguments are handled in the manner of fmt.Println.

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug logs detailed information that could mainly be used to catch unforeseen problems. Arguments are handled in the manner of fmt.Print.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, v ...interface{})

Debugf logs detailed information that could mainly be used to catch unforeseen problems. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Debugj

func (l *Logger) Debugj(msg string, v interface{})

Debugj logs detailed information that could mainly be used to catch unforeseen problems. Argument v becomes jsonPayload field in the log entry.

func (*Logger) Debugln

func (l *Logger) Debugln(v ...interface{})

Debugln logs detailed information that could mainly be used to catch unforeseen problems. Arguments are handled in the manner of fmt.Println.

func (*Logger) Emergency

func (l *Logger) Emergency(v ...interface{})

Emergency logs when one or more systems are unusable. Arguments are handled in the manner of fmt.Print.

func (*Logger) Emergencyf

func (l *Logger) Emergencyf(format string, v ...interface{})

Emergencyf logs when one or more systems are unusable. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Emergencyj

func (l *Logger) Emergencyj(msg string, v interface{})

Emergencyj logs when one or more systems are unusable. Argument v becomes the jsonPayload field of the log entry.

func (*Logger) Emergencyln

func (l *Logger) Emergencyln(v ...interface{})

Emergencyln logs when one or more systems are unusable. Arguments are handled in the manner of fmt.Println.

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error logs events likely to cause problems. Arguments are handled in the manner of fmt.Print.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, v ...interface{})

Errorf logs events likely to cause problems. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Errorj

func (l *Logger) Errorj(msg string, v interface{})

Errorj logs events likely to cause problems. Argument v becomes the jsonPayload field of the log entry.

func (*Logger) Errorln

func (l *Logger) Errorln(v ...interface{})

Errorln logs events likely to cause problems. Arguments are handled in the manner of fmt.Println.

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

Fatal is equivalent to a call to l.Critical() followed by a call to os.Exit(1).

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, v ...interface{})

Fatalf is equivalent to a call to l.Criticalf() followed by a call to os.Exit(1).

func (*Logger) Fatalj

func (l *Logger) Fatalj(msg string, v interface{})

Fatalj is equivalent to a call to l.Criticalj() followed by a call to os.Exit(1).

func (*Logger) Fatalln

func (l *Logger) Fatalln(v ...interface{})

Fatalln is equivalent to a call to l.Criticalln() followed by a call to os.Exit(1).

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info logs routine information, such as ongoing status or performance. Arguments are handled in the manner of fmt.Print.

func (*Logger) Infof

func (l *Logger) Infof(format string, v ...interface{})

Infof logs routine information, such as ongoing status or performance. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Infoj

func (l *Logger) Infoj(msg string, v interface{})

Infoj logs routine information, such as ongoing status or performance. Argument v becomes the jsonPayload field of the log entry.

func (*Logger) Infoln

func (l *Logger) Infoln(v ...interface{})

Infoln logs routine information, such as ongoing status or performance. Arguments are handled in the manner of fmt.Println.

func (*Logger) Notice

func (l *Logger) Notice(v ...interface{})

Notice logs normal but significant events, such as start up, shut down, or configuration. Arguments are handled in the manner of fmt.Print.

func (*Logger) Noticef

func (l *Logger) Noticef(format string, v ...interface{})

Noticef logs normal but significant events, such as start up, shut down, or configuration. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Noticej

func (l *Logger) Noticej(msg string, v interface{})

Noticej logs normal but significant events, such as start up, shut down, or configuration. Argument v becomes the jsonPayload field of the log entry.

func (*Logger) Noticeln

func (l *Logger) Noticeln(v ...interface{})

Noticeln logs normal but significant events, such as start up, shut down, or configuration. Arguments are handled in the manner of fmt.Println.

func (*Logger) Panic

func (l *Logger) Panic(v ...interface{})

Panic is equivalent to a call to l.Critical() followed by a call to panic().

func (*Logger) Panicf

func (l *Logger) Panicf(format string, v ...interface{})

Panicf is equivalent to a call to l.Criticalf() followed by a call to panic().

func (*Logger) Panicj

func (l *Logger) Panicj(msg string, v interface{})

Panicj is equivalent to a call to l.Criticalj() followed by a call to panic().

func (*Logger) Panicln

func (l *Logger) Panicln(v ...interface{})

Panicln is equivalent to a call to l.Criticalln() followed by a call to panic().

func (*Logger) Print

func (l *Logger) Print(v ...interface{})

Print logs routine information, such as ongoing status or performance, same as l.Info(). Arguments are handled in the manner of fmt.Print.

func (*Logger) Printf

func (l *Logger) Printf(format string, v ...interface{})

Printf logs routine information, such as ongoing status or performance, same as l.Infof(). Arguments are handled in the manner of fmt.Printf.

func (*Logger) Printj

func (l *Logger) Printj(msg string, v interface{})

Printj logs routine information, such as ongoing status or performance, same as l.Infoj(). Argument v becomes the jsonPayload field of the log entry.

func (*Logger) Println

func (l *Logger) Println(v ...interface{})

Println logs routine information, such as ongoing status or performance, same as l.Infoln(). Arguments are handled in the manner of fmt.Println.

func (*Logger) Warning

func (l *Logger) Warning(v ...interface{})

Warning logs events that might cause problems. Arguments are handled in the manner of fmt.Print.

func (*Logger) Warningf

func (l *Logger) Warningf(format string, v ...interface{})

Warningf logs events that might cause problems. Arguments are handled in the manner of fmt.Printf.

func (*Logger) Warningj

func (l *Logger) Warningj(msg string, v interface{})

Warningj logs events that might cause problems. Argument v becomes the jsonPayload field of the log entry.

func (*Logger) Warningln

func (l *Logger) Warningln(v ...interface{})

Warningln logs events that might cause problems. Arguments are handled in the manner of fmt.Println.

Jump to

Keyboard shortcuts

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