log

package module
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README ΒΆ

πŸ“œ Log Package

Generic badge Generic badge Generic badge

πŸ“Œ Getting Started

go get -u github.com/gerins/log
Echo
func main() {
	// Using default configuration, please use InitWithConfig() for production environment.
	log.Init() 
	e := echo.New()

	// Init logging middleware
	e.Use(middlewareLog.SetLogRequest())                       // Mandatory
	e.Use(middleware.BodyDump(middlewareLog.SaveLogRequest())) // Mandatory

	// Init handler
	e.GET("/", func(c echo.Context) error {
		// Get context from echo locals.
		ctx := c.Get("ctx").(context.Context)

		// Assign user id to Log Request model
		// So wen can know who make the request to the server
		log.Context(ctx).UserID = 2020

		// Log Request
		log.Context(ctx).Debug("Testing Log Request Debug")
		log.Context(ctx).Info("Testing Log Request Info")
		log.Context(ctx).Warn("Testing Log Request Warn")
		log.Context(ctx).Error("Testing Log Request Error")

		// Global log
		log.Debug("Testing Global Log Debug")
		log.Info("Testing Global Log Info")
		log.Warn("Testing Global Log Warn")
		log.Error("Testing Global Log Error")

		return c.String(http.StatusOK, "Hello, Log!")
	})

	e.Start("localhost:8080")
}

πŸ€ Sample Log Request

{
   "level": "info",
   "time": "2021-10-23T15:02:53.189+0700",
   "caller": "echo/log_request.go:59",
   "msg": "REQUEST_LOG",
   "ProcessID": "JLFyq8YcfN5KOy9knY42",
   "UserID": 2020,
   "IP": "127.0.0.1",
   "Method": "GET",
   "URL": "localhost:8080/",
   "RequestHeader": {
      "Accept": ["*/*"],
      "User-Agent": ["curl/7.68.0"]
   },
   "RequestBody": {},
   "ResponseHeader": {
      "Content-Type": ["text/plain; charset=UTF-8"]
   },
   "ResponseBody": null,
   "StatusCode": 200,
   "RequestDuration": 1,
   "ExtraData": null,
   "SubLog": [
      {
         "level": "DEBUG",
         "message": "Testing Log Request Debug"
      },
      {
         "level": "INFO",
         "message": "Testing Log Request Info"
      },
      {
         "level": "WARN",
         "message": "Testing Log Request Warn"
      },
      {
         "level": "ERROR",
         "message": "Testing Log Request Error"
      }
   ]
}

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var (
	DefaultLogConfig = Config{
		LogToTerminal: true,
		Location:      "/log/",
		FileLogName:   "server_log",
		FileFormat:    ".%Y-%b-%d-%H-%M.log",
		MaxAge:        30,
		RotationFile:  24,
		UseStackTrace: false,
	}
)

Functions ΒΆ

func Context ΒΆ

func Context(ctx context.Context) *request

Context is used for get log request model from context

func Debug ΒΆ

func Debug(i ...interface{})

func Debugf ΒΆ

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

func Error ΒΆ

func Error(i ...interface{})

func Errorf ΒΆ

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

func Fatal ΒΆ

func Fatal(i ...interface{})

func Fatalf ΒΆ

func Fatalf(msg string, i ...interface{})

func GetCaller ΒΆ

func GetCaller(level string, skip int) string

GetCaller return trimmed location of the file who call the log function example project_name/usecase/user.go:34

func Info ΒΆ

func Info(i ...interface{})

func Infof ΒΆ

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

func Init ΒΆ

func Init()

func InitWithConfig ΒΆ

func InitWithConfig(cfg Config)

func NewRequest ΒΆ

func NewRequest() *request

NewRequest will create new log data model for incoming request

func Tracing ΒΆ

func Tracing(processId, url, method string, resCode int, resPayload []byte, reqHeader, payload, respHeader interface{}, err error, dur int64)

Special log only for tracing service to service communication

func Warn ΒΆ

func Warn(i ...interface{})

func Warnf ΒΆ

func Warnf(format string, i ...interface{})

Types ΒΆ

type Config ΒΆ

type Config struct {
	LogToTerminal bool   // Highly recommended disable in production server. Default true.
	Location      string // Location file log will be save. Default "project_directory/log/".
	FileLogName   string // File log name. Default "server_log".
	FileFormat    string // Default "FileLogName.2021-Oct-22-00-00.log"
	MaxAge        int    // Days before deleting log file. Default 30 days.
	RotationFile  int    // Hour before creating new file. Default 24 hour.
	UseStackTrace bool   // Print stack trace to log file. Default false.
}

Directories ΒΆ

Path Synopsis
example
extension
middleware

Jump to

Keyboard shortcuts

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