github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/wordsmith/log/logger.go (about)

     1  // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
     2  //
     3  // This software (Documize Community Edition) is licensed under 
     4  // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
     5  //
     6  // You can operate outside the AGPL restrictions by purchasing
     7  // Documize Enterprise Edition and obtaining a commercial license
     8  // by contacting <sales@documize.com>. 
     9  //
    10  // https://documize.com
    11  
    12  // Package log provides centralized logging for the Documize application.
    13  package log
    14  
    15  import (
    16  	"bytes"
    17  	"fmt"
    18  	"os"
    19  	"runtime"
    20  
    21  	log "github.com/Sirupsen/logrus"
    22  
    23  	env "github.com/documize/community/wordsmith/environment"
    24  )
    25  
    26  var environment = "Non-production"
    27  
    28  func init() {
    29  	log.SetFormatter(new(log.TextFormatter))
    30  	log.SetOutput(os.Stdout)
    31  	log.SetLevel(log.DebugLevel)
    32  	env.GetString(&environment, "log", false,
    33  		"system being logged e.g. 'PRODUCTION'",
    34  		func(*string, string) bool {
    35  			log.Infoln(environment + " environment logging enabled")
    36  			return false
    37  		})
    38  }
    39  
    40  // Debug logs a message for debug purposes.
    41  func Debug(message string) {
    42  	log.WithFields(log.Fields{"env": environment}).Debug(message)
    43  }
    44  
    45  // Info logs a message for information purposes.
    46  func Info(message string) {
    47  	log.WithFields(log.Fields{"env": environment}).Info(message)
    48  }
    49  
    50  // TestIfErr is used by the test code to signal that a test being run should error, it is reset if an error occurs.
    51  var TestIfErr bool
    52  
    53  // ErrorString logs an error, where there is not an error value.
    54  func ErrorString(message string) {
    55  	TestIfErr = false
    56  	log.WithFields(log.Fields{"env": environment}).Error(message)
    57  }
    58  
    59  // Error logs an error, if non-nil, with a message to give some context.
    60  func Error(message string, err error) {
    61  	if err != nil {
    62  		TestIfErr = false
    63  		stack := make([]byte, 4096)
    64  		runtime.Stack(stack, false)
    65  		if idx := bytes.IndexByte(stack, 0); idx > 0 && idx < len(stack) {
    66  			stack = stack[:idx] // remove trailing nulls from stack dump
    67  		}
    68  		log.WithFields(log.Fields{"env": environment, "error": err.Error(), "stack": fmt.Sprintf("%s", stack)}).Error(message)
    69  
    70  		//log.WithField("error: "+message, err.Error()).Errorf("%q\n%s\n", err, stack[:])
    71  	}
    72  }
    73  
    74  // IfErr logs an error if one exists.
    75  // It is a convenience wrapper for Error(), with no context message.
    76  func IfErr(err error) {
    77  	Error("", err)
    78  }