log

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

go-cm/log

  • ZeroLog integration
  • Support request logging with Resty

Installation

go get -u github.com/phe-lab/go-cm/log

Usage

Quickstart
package main

import "github.com/phe-lab/go-cm/log"

func main() {
	logger := log.NewLogger()
	logger.Debug().Str("host", "0.0.0.0").Int("port", 3000).Send()
	logger.Info().Msg("Application starting")
}
# output:
{"level":"debug","host":"0.0.0.0","port":3000,"time":"2024-11-16T13:38:53+07:00"}
{"level":"info","time":"2024-11-16T13:38:53+07:00","message":"Application starting"}
Global setup
package main

import "github.com/phe-lab/go-cm/log"

func main() {
	log.WithCaller(true)
	log.SetGlobalFormat("json")
	log.SetGlobalLevel("info")

    logger := log.NewLogger()
	logger.Debug().Str("host", "0.0.0.0").Int("port", 3000).Send()
	logger.Info().Msg("Application starting")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger() zerolog.Logger

NewLogger creates a new instance of a zerolog.Logger configured with global settings.

The logger includes timestamps by default. If `withCaller` is enabled, it also includes caller information (file and line number).

Returns: - A new instance of zerolog.Logger.

func SetGlobalFormat

func SetGlobalFormat(format string)

SetGlobalFormat sets the global log output format.

Supported formats: - "json": Logs will be written in JSON format (default). - Any other value: Logs will be written in a human-readable console format.

Parameters: - format: The desired log format ("json" or others).

func SetGlobalLevel

func SetGlobalLevel(levelStr string)

SetGlobalLevel sets the global log level.

Supported levels: - "debug", "info", "warn", "error", "fatal", "panic", "trace".

If an invalid level string is provided, the log level defaults to "info", and a warning message is logged.

Parameters: - levelStr: The desired global log level as a string.

func WithCaller

func WithCaller(enabled bool)

WithCaller enables or disables the inclusion of caller information (file and line number) in logs.

Parameters: - enabled: If true, logs will include caller information.

Types

type RestyTracer

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

RestyTracer is a custom tracer for Resty HTTP client, designed to log detailed trace information about HTTP requests and responses.

It leverages zerolog for structured logging and captures information such as DNS lookup time, connection time, TLS handshake duration, and more.

func NewRestyTracer

func NewRestyTracer(logger zerolog.Logger) *RestyTracer

NewRestyTracer creates a new instance of RestyTracer.

Parameters: - logger: A zerolog.Logger instance to be used for logging.

Returns: - A pointer to the newly created RestyTracer.

func (*RestyTracer) Trace

func (t *RestyTracer) Trace(r *resty.Response)

Trace logs detailed information about an HTTP request and its trace details using zerolog.

This method captures the following details: - Request URL, method, and status code. - Protocol used for the request. - Request and response headers. - TraceInfo metrics such as DNS lookup time, connection time, and more.

Parameters: - r: The Resty Response object containing the HTTP request and trace details.

type TraceInfo

type TraceInfo struct {
	DNSLookup      string
	ConnTime       string
	TCPConnTime    string
	TLSHandshake   string
	ServerTime     string
	ResponseTime   string
	TotalTime      string
	IsConnReused   bool
	IsConnWasIdle  bool
	ConnIdleTime   string
	RequestAttempt int
	RemoteAddr     net.Addr
}

TraceInfo represents detailed timing and connection metrics for an HTTP request. It mirrors the trace information provided by Resty's TraceInfo, with fields formatted as strings for structured logging purposes.

Jump to

Keyboard shortcuts

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