-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: David Jimenez <dvejmz@sgfault.com>
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ revive | |
vendor | ||
*.swp | ||
dist/ | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package logging | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
) | ||
|
||
var logger *log.Logger | ||
|
||
// GetLogger retrieves an instance of an application logger which outputs | ||
// to a file if the debug flag is enabled | ||
func GetLogger() (*log.Logger, error) { | ||
if logger != nil { | ||
return logger, nil | ||
} | ||
|
||
var writer io.Writer | ||
var err error | ||
|
||
debugModeEnabled := os.Getenv("DEBUG") == "1" | ||
if debugModeEnabled { | ||
writer, err = os.Create("revive.log") | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
// Suppress all logging output if debug mode is disabled | ||
writer = ioutil.Discard | ||
} | ||
|
||
logger = log.New(writer, "", log.LstdFlags) | ||
|
||
if !debugModeEnabled { | ||
// Clear all flags to skip log output formatting step to increase | ||
// performance somewhat if we're not logging anything | ||
logger.SetFlags(0) | ||
} | ||
|
||
logger.Println("Logger initialised") | ||
|
||
return logger, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters