Code Monkey home page Code Monkey logo

log's Introduction

Go Report Card License License Stay with Ukraine

log

The log module encompasses methods for comprehensive logging, which comprises a variety of logging levels:

  • Panic Panic typically signifies that something has gone unexpectedly awry. It's primarily utilized to swiftly halt on errors that shouldn't surface during regular operation, or those we aren't equipped to handle smoothly.

  • Fatal Fatal corresponds to situations that are immensely disastrous for the application. The application is on the verge of terminating to avert any sort of corruption or severe problem, if feasible. Exit code is 1.

  • Error An error represents a significant issue and depicts the failure of something crucial within an application. Contrary to FATAL, the application itself is not doomed.

  • Warn This log level implies that an application might be experiencing a problem and an unusual situation has been detected. It's an unexpected and unusual issue, but no real damage is done, and it's uncertain whether the problem will persist or happen again.

  • Info The messages at this level relate to standard application behavior and milestones. They offer a framework of the events that took place.

  • Debug This level is meant to provide more detailed, diagnostic information than the INFO level.

  • Trace This level offers incredibly detailed information, even more so than DEBUG. At this level, every possible detail about the application's behavior should be captured.

Installation

To install this module use go get as:

$ go get -u github.com/goloop/log

Quick Start

To use this module import it as:

package main

import (
	"os"

	"github.com/goloop/log"
	"github.com/goloop/log/layout"
	"github.com/goloop/log/level"

	// It is not necessary to import this module, the three-valued logic
	// constants can be represented using integers where: -1 is False,
	// 0 is Unknown, and 1 is True.
	"github.com/goloop/trit"
)

func main() {
	// Open the file in append mode. Create the file if it doesn't exist.
	// Use appropriate permissions in a production setting.
	file, err := os.OpenFile(
		"errors.log",
		os.O_APPEND|os.O_CREATE|os.O_WRONLY,
		0644,
	)

	if err != nil {
		log.Fatal(err)
	}

	// Defer the closing of the file until the function ends.
	defer file.Close()

	// Set the outputs of the log to our file.
	// We can set many different outputs to record
	// individual errors, debug or mixed data.
	err = log.SetOutputs(
		log.Output{
			Name:      "stdout",
			Writer:    os.Stdout,
			Levels:    level.Debug | level.Info | level.Warn | level.Error,
			Layouts:   layout.Default,
			WithColor: 1, // or trit.True, see github.com/goloop/trit
		},
		log.Output{
			Name:      "errorsJSONFile",
			Writer:    file,
			Levels:    level.Warn | level.Error, // only errors and warnings
			Layouts:   layout.Default,
			TextStyle: trit.False, // or just -1
		},
	)

	if err != nil {
		log.Fatal(err)
	}

	// A message will be output to a file and to the console.
	// * stdout and errorsJSONFile has Error level.
	log.Errorln("This is a test log message with ERROR.")

	// A message will be output to the console only.
	// * stdout has Debug level, but errorsJSONFile has not.
	log.Debugln("This is a test log message with DEBUG.")
}

log's People

Contributors

goloop avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.