log

package
v0.0.0-...-7fb663c Latest Latest
Warning

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

Go to latest
Published: May 3, 2020 License: MPL-2.0 Imports: 6 Imported by: 0

README

gogo/log

A golang package for formatted log messages that works across Linux and Windows supporting ANSI-colors.

gogo/log provides a consistent API for formatting Log Messages (stdout) in Golang Command Line Interface Projects.

Package Dependencies

Basic Usage

Here is a quick summary of basic usage

import "github.com/knowntraveler/gogo/log"

func main() {
    
    // Standard Log Messages
    log.Print("This is a standard log message")
    log.Printf("This is a standard log message with %s\n", "formatting")

    // Verbose Log Messages -> EnableVerbose()
    log.VPrint("This is a verbose log message")
    log.VPrintf("This is a verbose log message with %s\n", "formatting")

    // Debug Log Messages -> EnableDebug()
    log.Debug("This is a debug log message")
    log.Debug("This is a debug log message with %s\n", "formatting")

    // Trace Log Messages -> EnableTrace()
    log.Trace("This is a trace log message")
    log.Tracef("This is a trace log message with %s\n", "formatting")

    // Success Log Messages
    log.Success("This is a success log message")
    log.Successf("This is a success log message with %s\n", "formatting")

    // Warning Log Messages
    log.Warning("This is a warning log message")
    log.Warningf("This is a warning log message with %s\n", "formatting")

    // Failure Log Messages
    log.Failure("This is a failure log message")
    log.Failuref("This is a failure log message with %s\n", "formatting")

    // Error Log Messages
    log.Error("This is an error log message")
    log.Errorf("This is an errorlog message with %s\n", "formatting")

    // Panic Log Messages
    log.Panic("This is a panic log message")
    log.Panicf("This is a panic log message with %s\n", "formatting")

    // Fatal Log Messages
    log.Fatal("This is a fatal log message")
    log.Fatalf("This is a fatal log message with %s\n", "formatting")     
}

Practical Example

When developing Command Line Utilities (CLI) using Cobra/Viper you can do the following in root.go with package gogo/log

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
    Use:   "myCommand",
    Short: "A brief description of your application",
    Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,

    PersistentPreRun: func(cmd *cobra.Command, args []string) {

        // Configure Verbose Logging Option from CLI
        if options.verbose {
            log.EnableVerbose()
        }

        // Configure Logging options from Environment
        logLevel := os.Getenv("MYCOMMAND_LOG_LEVEL")

        if logLevel == "verbose" {
            log.EnableVerbose()
        }

        if logLevel == "debug" {
            log.EnableVerbose()
            log.EnableDebug()
        }

        if logLevel == "trace" {
            log.EnableVerbose()
            log.EnableDebug()
            log.EnableTrace()
        }

    },
}

type rootFlags struct {
	verbose              bool
    nonInteractive       bool
    overrideConfirmation bool
}   

var options rootFlags

func init() {
    // Commands

    // PersistentFlags
    rootCmd.PersistentFlags().BoolVarP(&options.verbose, "verbose", "v", false, "Enable verbose output")
    rootCmd.PersistentFlags().BoolVar(&options.nonInteractive, "non-interactive", false, "Disable interactive prompts")
    rootCmd.PersistentFlags().BoolVarP(&options.overrideConfirmation, "yes", "y", false, "Override confirmations")
}

Running Unit Tests

go test -v
=== RUN   TestPrint
--- PASS: TestPrint (0.00s)
=== RUN   TestPrintf
--- PASS: TestPrintf (0.00s)
=== RUN   TestVPrint
--- PASS: TestVPrint (0.00s)
=== RUN   TestVPrintf
--- PASS: TestVPrintf (0.00s)
=== RUN   TestDebug
--- PASS: TestDebug (0.00s)
=== RUN   TestDebugf
--- PASS: TestDebugf (0.00s)
=== RUN   TestTrace
--- PASS: TestTrace (0.00s)
=== RUN   TestTracef
--- PASS: TestTracef (0.00s)
=== RUN   TestSuccess
--- PASS: TestSuccess (0.00s)
=== RUN   TestSuccessf
--- PASS: TestSuccessf (0.00s)
=== RUN   TestWarning
--- PASS: TestWarning (0.00s)
=== RUN   TestWarningf
--- PASS: TestWarningf (0.00s)
=== RUN   TestFailure
--- PASS: TestFailure (0.00s)
=== RUN   TestFailuref
--- PASS: TestFailuref (0.00s)
=== RUN   TestError
--- PASS: TestError (0.00s)
=== RUN   TestErrorf
--- PASS: TestErrorf (0.00s)
PASS
ok      github.com/knowntraveler/gogo/log       0.002s

Documentation

Overview

Package log provides a uniform logging api for formatting log output on linux/windows

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(message string)

Debug logs a message at level Debug

func Debugf

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

Debugf logs a formatted message at level Debug

func EnableDebug

func EnableDebug()

EnableDebug turns on enable logging

func EnableTrace

func EnableTrace()

EnableTrace turns on trace logging

func EnableVerbose

func EnableVerbose()

EnableVerbose turns on verbose logging

func Error

func Error(message string)

Error logs a message at level Error

func Errorf

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

Errorf logs a message at level Error

func Failure

func Failure(message string)

Failure logs a message at level Error

func Failuref

func Failuref(format string, args ...interface{})

Failuref logs a formatted message at level Error

func Fatal

func Fatal(message string)

Fatal logs a message at level Fatal

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs a formatted message at level Fatal

func Panic

func Panic(message string)

Panic logs a message at level Panic

func Panicf

func Panicf(format string, args ...interface{})

Panicf logs a formatted message at level Panic

func Print

func Print(message string)

Print logs a message at level Info

func Printf

func Printf(format string, args ...interface{})

Printf logs a formatted message at level Info

func Success

func Success(message string)

Success logs a message at level Info

func Successf

func Successf(format string, args ...interface{})

Successf logs a formatted message at level Info

func Trace

func Trace(message string)

Trace logs a message at level Trace

func Tracef

func Tracef(format string, args ...interface{})

Tracef logs a formatted message at level Trace

func VPrint

func VPrint(message string)

VPrint logs a message at level Info when verboseEnabled is true

func VPrintf

func VPrintf(format string, args ...interface{})

VPrintf logs a message at level Info when verboseEnabled is true

func Warning

func Warning(message string)

Warning logs a message at level Warn

func Warningf

func Warningf(format string, args ...interface{})

Warningf logs a formatted message at level Warn

Types

This section is empty.

Jump to

Keyboard shortcuts

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