logger.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package logger
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "time"
  9. )
  10. /*
  11. Zoraxy Logger
  12. This script is designed to make a managed log for the Zoraxy
  13. and replace the ton of log.Println in the system core
  14. */
  15. type Logger struct {
  16. LogToFile bool //Set enable write to file
  17. Prefix string //Prefix for log files
  18. LogFolder string //Folder to store the log file
  19. CurrentLogFile string //Current writing filename
  20. file *os.File
  21. }
  22. func NewLogger(logFilePrefix string, logFolder string, logToFile bool) (*Logger, error) {
  23. err := os.MkdirAll(logFolder, 0775)
  24. if err != nil {
  25. return nil, err
  26. }
  27. thisLogger := Logger{
  28. LogToFile: logToFile,
  29. Prefix: logFilePrefix,
  30. LogFolder: logFolder,
  31. }
  32. logFilePath := thisLogger.getLogFilepath()
  33. f, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
  34. if err != nil {
  35. return nil, err
  36. }
  37. thisLogger.CurrentLogFile = logFilePath
  38. thisLogger.file = f
  39. return &thisLogger, nil
  40. }
  41. func (l *Logger) getLogFilepath() string {
  42. year, month, _ := time.Now().Date()
  43. return filepath.Join(l.LogFolder, l.Prefix+"_"+strconv.Itoa(year)+"-"+strconv.Itoa(int(month))+".log")
  44. }
  45. // PrintAndLog will log the message to file and print the log to STDOUT
  46. func (l *Logger) PrintAndLog(title string, message string, originalError error) {
  47. go func() {
  48. l.Log(title, message, originalError)
  49. }()
  50. log.Println("[" + title + "] " + message)
  51. }
  52. // Println is a fast snap-in replacement for log.Println
  53. func (l *Logger) Println(v ...interface{}) {
  54. //Convert the array of interfaces into string
  55. message := fmt.Sprint(v...)
  56. go func() {
  57. l.Log("info", string(message), nil)
  58. }()
  59. log.Println("[INFO] " + string(message))
  60. }
  61. func (l *Logger) Log(title string, errorMessage string, originalError error) {
  62. l.ValidateAndUpdateLogFilepath()
  63. if l.LogToFile {
  64. if originalError == nil {
  65. l.file.WriteString(time.Now().Format("2006-01-02 15:04:05.000000") + "|" + fmt.Sprintf("%-16s", title) + " [INFO]" + errorMessage + "\n")
  66. } else {
  67. l.file.WriteString(time.Now().Format("2006-01-02 15:04:05.000000") + "|" + fmt.Sprintf("%-16s", title) + " [ERROR]" + errorMessage + " " + originalError.Error() + "\n")
  68. }
  69. }
  70. }
  71. // Validate if the logging target is still valid (detect any months change)
  72. func (l *Logger) ValidateAndUpdateLogFilepath() {
  73. expectedCurrentLogFilepath := l.getLogFilepath()
  74. if l.CurrentLogFile != expectedCurrentLogFilepath {
  75. //Change of month. Update to a new log file
  76. l.file.Close()
  77. f, err := os.OpenFile(expectedCurrentLogFilepath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
  78. if err != nil {
  79. log.Println("[Logger] Unable to create new log. Logging to file disabled.")
  80. l.LogToFile = false
  81. return
  82. }
  83. l.CurrentLogFile = expectedCurrentLogFilepath
  84. l.file = f
  85. }
  86. }
  87. func (l *Logger) Close() {
  88. l.file.Close()
  89. }