Yet another Go logging library for user-friendly applications, with colors!

levels.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package log
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. type logLevel int
  7. // Log levels for controlling whether or not logs of certain types will be
  8. // emitted by your logger.
  9. const (
  10. DebugLevel logLevel = iota
  11. InfoLevel
  12. WarnLevel
  13. ErrorLevel
  14. FatalLevel
  15. )
  16. // Map log levels to human readable labels.
  17. var levelNames = map[logLevel]string{
  18. DebugLevel: "DEBUG",
  19. InfoLevel: "INFO",
  20. WarnLevel: "WARN",
  21. ErrorLevel: "ERROR",
  22. FatalLevel: "FATAL",
  23. }
  24. // emit is the general purpose log line emitter.
  25. func (l *Logger) emit(level logLevel, tmpl string, args ...interface{}) {
  26. message := l.Format(level, tmpl, args...)
  27. // If we have a log writer, send it there.
  28. if l.Config.Writer != nil {
  29. // TODO
  30. // l.Config.Writer.Write(message)
  31. } else {
  32. // No writer given so we default to standard out/error.
  33. if level <= InfoLevel {
  34. fmt.Fprintln(os.Stdout, message)
  35. } else {
  36. fmt.Fprintln(os.Stderr, message)
  37. }
  38. }
  39. }
  40. // Debug emits a debug-level message from the logger.
  41. func (l *Logger) Debug(tmpl string, args ...interface{}) {
  42. if l.Config.Level <= DebugLevel {
  43. l.emit(DebugLevel, tmpl, args...)
  44. }
  45. }
  46. // Info emits an informational message.
  47. func (l *Logger) Info(tmpl string, args ...interface{}) {
  48. if l.Config.Level <= InfoLevel {
  49. l.emit(InfoLevel, tmpl, args...)
  50. }
  51. }
  52. // Warn emits a warning message.
  53. func (l *Logger) Warn(tmpl string, args ...interface{}) {
  54. if l.Config.Level <= WarnLevel {
  55. l.emit(WarnLevel, tmpl, args...)
  56. }
  57. }
  58. // Error emits an error message.
  59. func (l *Logger) Error(tmpl string, args ...interface{}) {
  60. if l.Config.Level <= ErrorLevel {
  61. l.emit(ErrorLevel, tmpl, args...)
  62. }
  63. }
  64. // Fatal logs a fatal message and quits.
  65. func (l *Logger) Fatal(tmpl string, args ...interface{}) {
  66. l.emit(FatalLevel, tmpl, args...)
  67. }