You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.9 KiB

  1. package main
  2. //import "time"
  3. import "os"
  4. import "github.com/romana/rlog"
  5. func someRecursiveFunction(x int) {
  6. // The Trace log functions allow you to set a log level with a numeric
  7. // level, which could be determined at run time. Here, we are tracing or
  8. // logging the first few levels of the recursive function (to whatever
  9. // levels was determined via the RLOG_TRACE_LEVEL variable), but after that
  10. // will stop logging, even though the function itself may decend further.
  11. rlog.Tracef(x, "We're %d levels down now...", x)
  12. if x < 10 {
  13. someRecursiveFunction(x + 1)
  14. } else {
  15. rlog.Infof("Reached end of recursion at level %d", x)
  16. }
  17. }
  18. func main() {
  19. // Show different log levels
  20. rlog.Info("Start of program")
  21. rlog.Info("rlog is controlled via environment variables.")
  22. rlog.Info("Try the following settings:")
  23. rlog.Info(" export RLOG_LOG_LEVEL=DEBUG")
  24. rlog.Info(" export RLOG_TRACE_LEVEL=5")
  25. rlog.Info(" export RLOG_CALLER_INFO=yes")
  26. rlog.Debug("You only see this if you did 'export RLOG_LOG_LEVEL=DEBUG'")
  27. rlog.Infof("Format %s are possible %d", "strings", 123)
  28. rlog.Warn("Warning level log message")
  29. rlog.Error("Error level log message")
  30. rlog.Critical("Critical level log message")
  31. // Example of how to change settings programmatically from within
  32. // the running program: Modify your own environment variables.
  33. // Note that if the config file specifies the value with a '!' then
  34. // this value cannot be changed by modifying the environment variable.
  35. rlog.Debug("You can't see this if the log level is higher than DEBUG.")
  36. os.Setenv("RLOG_LOG_LEVEL", "DEBUG")
  37. rlog.UpdateEnv()
  38. rlog.Debug("You can see this message, because we changed level to DEBUG.")
  39. // Example of selective trace logging
  40. rlog.Trace(1, "Trace messages have their own numeric levels")
  41. rlog.Trace(1, "To see them set RLOG_TRACE_LEVEL to the cut-off number")
  42. someRecursiveFunction(1)
  43. // Example of redirecting log output to a new file at runtime
  44. newLogFile, err := os.OpenFile("/tmp/rlog-output.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
  45. if err == nil {
  46. rlog.Info("About to change log output. Check /tmp/rlog-output.log...")
  47. rlog.SetOutput(newLogFile)
  48. rlog.Info("This should go to the new logfile")
  49. }
  50. // Now redirecting it back to stderr. Any additional file logging will be
  51. // closed with this call.
  52. rlog.SetOutput(os.Stderr)
  53. rlog.Info("Back to stderr")
  54. // To test out changing the log configuration of a running process, set the
  55. // RLOG_CONF_FILE environment variable to an absolute path for a
  56. // configuration file that you can create and edit. Also, you might want to
  57. // set RLOG_CONF_CHECK_INTERVAL to a smaller number, so you don't have to
  58. // wait 15 seconds every time you change a setting.
  59. // Then uncomment the loop below (and the 'time' import at the top) and run
  60. // the example program.
  61. /*
  62. rlog.SetConfFile("some-other-rlog.conf")
  63. for {
  64. time.Sleep(time.Second * 1)
  65. rlog.Debug("A debug log message")
  66. rlog.Info("An info log message")
  67. }
  68. */
  69. }