Documentation
¶
Overview ¶
Package logz provides the structured log with the OpenTelemetry.
Example:
ctx := r.Context() // r is *http.Request logz.Infof(ctx, "info log. requestURL: %s", r.URL.String())
Example ¶
package main
import (
"log"
"net/http"
"github.com/devotedalpha/logz"
"github.com/devotedalpha/logz/middleware"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Writes info log
logz.Infof(ctx, "writes %s log", "info")
})
logz.SetProjectID("your gcp project id")
logz.InitTracer()
// Sets the middleware
h := middleware.NetHTTP("tracer name")(mux)
log.Fatal(http.ListenAndServe(":8080", h))
}
Output:
Index ¶
- func Access(ctx context.Context, r http.Request, statusCode, responseSize int, ...)
- func AccessLog(ctx context.Context, method, url, userAgent, remoteIP, protocol string, ...)
- func Criticalf(ctx context.Context, format string, a ...interface{})
- func Debugf(ctx context.Context, format string, a ...interface{})
- func Errorf(ctx context.Context, format string, a ...interface{})
- func Infof(ctx context.Context, format string, a ...interface{})
- func InitTracer()
- func SetConfig(cfg Config)
- func SetProjectID(projectID string)
- func StartCollectingSeverity(ctx context.Context) context.Context
- func Warningf(ctx context.Context, format string, a ...interface{})
- type Config
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Access ¶
func Access(ctx context.Context, r http.Request, statusCode, responseSize int, elapsed time.Duration)
Access writes access log to the stderr
func AccessLog ¶
func AccessLog(ctx context.Context, method, url, userAgent, remoteIP, protocol string, statusCode, requestSize, responseSize int, elapsed time.Duration)
AccessLog writes access log to the stderr without http.Request
func SetConfig ¶
func SetConfig(cfg Config)
SetConfig sets config to the logger
Example ¶
package main
import (
"log"
"net/http"
"github.com/devotedalpha/logz"
"github.com/devotedalpha/logz/middleware"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Writes info log
logz.Infof(ctx, "writes %s log", "info")
})
logz.SetConfig(logz.Config{
ProjectID: "your gcp project id",
NeedsAccessLog: false, // Whether or not to write the access log
})
logz.InitTracer()
// Sets the middleware
h := middleware.NetHTTP("tracer name")(mux)
log.Fatal(http.ListenAndServe(":8080", h))
}
Output:
Example (ChangeLogout) ¶
package main
import (
"log"
"net/http"
"os"
"github.com/devotedalpha/logz"
"github.com/devotedalpha/logz/middleware"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Writes info log
logz.Infof(ctx, "writes %s log", "info")
})
// Writes log on local file
file, err := os.OpenFile("local.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
panic(err)
}
logz.SetConfig(logz.Config{
ProjectID: "your gcp project id",
NeedsAccessLog: false,
AccessLogOut: file,
ApplicationLogOut: file,
})
logz.InitTracer()
// Sets the middleware
h := middleware.NetHTTP("tracer name")(mux)
log.Fatal(http.ListenAndServe(":8080", h))
}
Output:
func SetProjectID ¶
func SetProjectID(projectID string)
SetProjectID sets gcp project id to the logger
func StartCollectingSeverity ¶
StartCollectingSeverity starts collectiong severity
Types ¶
type Config ¶
type Config struct {
// GCP Project ID
ProjectID string
// CallerDepth is the number of stack frames to ascend
CallerSkip int
// Whether or not to write the access log
NeedsAccessLog bool
// Output for application log
ApplicationLogOut io.Writer
// Output for access log
AccessLogOut io.Writer
}
Config is configurations for logz
Click to show internal directories.
Click to hide internal directories.
