Coder Social home page Coder Social logo

log's Introduction

Using gwaylib/log

The number of logs should not be too many in the release program, which may affect the program performance.
In the call, the logger adopts the function of asynchronous cache to improve the execution efficiency of the log. The cache value is determined by the adapter implements, and the default is 10 items cache.
The log should be closed after use to ensure that the cached data can be output. If the cached data cannot be output, the error will be transferred by the adapter.

About using log level

DEBUG
Debugging information, output only during debugging.
It is controlled by the platform and can be output to the console, which is equivalent to fmt.Print output.

INFO
Program running status change information.
Such as start, stop, reconnection and other information, which reflects the change state of the program environment.

WARN
Program exception information.
This category does not affect the continued use of the program, but its results may lead to potential major problems.
For example: some unknown errors; Network connection error (but it can be repaired automatically after reconnection), connection timeout, etc.
If there are too many such exceptions in a period of time, the causes should be analyzed, such as possible problems:
Being attacked, hardware aging, hardware reaching the upper limit of bearing capacity, abnormal service of the other party, etc.
The log adapter recommends sending an email to relevant personnel.

ERROR
Program fatal error message.
This error will affect the normal logic, and even the platform panic and stop the service.
For example, behaviors that need to be handled in time, such as database unavailable, recharge unavailable, SMS unavailable, and Vos unavailable, can be defined as this category.
The log adapter recommends sending an email, real-time SMS or telephone (or other real-time contact information) to notify relevant personnel.

FATAL
The program ended abnormally.
The log adapter recommends calling all real-time contact information and contacting relevant personnel for processing.

Default use case:

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

func main() {
  log.Info("OK")
}

Custom a logger

import (
  "github.com/gwaylib/log"
  "github.com/gwaylib/logger"
)

var lg *logger.Log

func init() {
  // make a custom logger 
  level, _ := strconv.Atoi(os.Getenv(log.GWAYLIB_LOG_LEVEL))
  adapter := []logger.Adapter{stdio.New(os.Stdout)}
  callerDepth := 4 // if callerDepth == 0, close the file path caller

  // replace the default logger
  log.Log = logger.New(&logger.DefaultContext, "appname", callerDepth, proto.Level(level), adapter...)

  // or
  // make a new package log
  lg = logger.New(&logger.DefaultContext, "appname", callerDepth, proto.Level(level), adapter...)

  // or
  // make a new package default log
  //lg = logger.NewDefaultLogger(loggerName, adapter...)

  // or 
  // copy the log.go file in local and fix it
}

func main() {
  log.Info("OK")
  lg.Info("OK")
}

log's People

Contributors

free1139 avatar metagates-dev avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.