2

I am running a task every 5 minutes and I want to log the error (if it faced the error) without exiting the program but the problem is log.Fatal() is exiting the program and log.Panic() will call panic() which again exits the program.

How can I log the error without exiting the program?

7
  • 1
    There is log.Print, log.Printf, and log.Println. Commented Mar 17, 2020 at 11:25
  • I am aware of them and I use them but they are for normal logs and not errors. In python, for example, there is a possibility to log error without exiting the program and the log is different so it is easy to find in the logs. Commented Mar 17, 2020 at 11:37
  • 1
    The log package does not provide a 4th variant. You have Print, Fatal, and Panic. And all of them are very basic, 2-3 lines-of-code functions, that just call fmt.Sprint and write the output of that to stdout. If you require behavior different from what's provided by log you can implement your own error logging function, just take a peek at those functions' source code, and write your own variant. Commented Mar 17, 2020 at 13:05
  • 2
    "they are for normal logs and not errors" How are errors different from "normal logs"? They just output text to the console. There's no difference. Commented Mar 17, 2020 at 13:05
  • The difference is the text formatting in the console and coloring. The library that I use in python print errors in red and in a different format and it is very easy to find them among hundreds of lines of logs. Another problem with log.Println/Printf is we have to always use it with if Commented Mar 17, 2020 at 13:10

1 Answer 1

10

You can use a different logging library like logrus or modify your standard log library like this:

log package:

// Info writes logs in the color blue with "INFO: " as prefix
var Info = log.New(os.Stdout, "\u001b[34mINFO: \u001B[0m", log.LstdFlags|log.Lshortfile)

// Warning writes logs in the color yellow with "WARNING: " as prefix
var Warning = log.New(os.Stdout, "\u001b[33mWARNING: \u001B[0m", log.LstdFlags|log.Lshortfile)

// Error writes logs in the color red with "ERROR: " as prefix
var Error = log.New(os.Stdout, "\u001b[31mERROR: \u001b[0m", log.LstdFlags|log.Lshortfile)

// Debug writes logs in the color cyan with "DEBUG: " as prefix
var Debug = log.New(os.Stdout, "\u001b[36mDEBUG: \u001B[0m", log.LstdFlags|log.Lshortfile)

with usage:

Error.Println("this is an error")

and the result would be:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

This really doens't answer the question I believe. Logrus still default exits on Fatal.
If what they are looking for is an easy to find error message, they could use the "Error" var from the example instead of using Fatal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.