logger.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package logger
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "time"
  9. )
  10. /*
  11. ArozOS System Logger
  12. This script is designed to make a managed log for the ArozOS system
  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. func (l *Logger) Log(title string, errorMessage string, originalError error) {
  53. l.ValidateAndUpdateLogFilepath()
  54. if l.LogToFile {
  55. if originalError == nil {
  56. l.file.WriteString(time.Now().Format("2006-01-02 15:04:05.000000") + "|" + fmt.Sprintf("%-16s", title) + " [INFO]" + errorMessage + "\n")
  57. } else {
  58. l.file.WriteString(time.Now().Format("2006-01-02 15:04:05.000000") + "|" + fmt.Sprintf("%-16s", title) + " [ERROR]" + errorMessage + " " + originalError.Error() + "\n")
  59. }
  60. }
  61. }
  62. //Validate if the logging target is still valid (detect any months change)
  63. func (l *Logger) ValidateAndUpdateLogFilepath() {
  64. expectedCurrentLogFilepath := l.getLogFilepath()
  65. if l.CurrentLogFile != expectedCurrentLogFilepath {
  66. //Change of month. Update to a new log file
  67. l.file.Close()
  68. f, err := os.OpenFile(expectedCurrentLogFilepath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
  69. if err != nil {
  70. log.Println("[Logger] Unable to create new log. Logging to file disabled.")
  71. l.LogToFile = false
  72. return
  73. }
  74. l.CurrentLogFile = expectedCurrentLogFilepath
  75. l.file = f
  76. }
  77. }
  78. func (l *Logger) Close() {
  79. l.file.Close()
  80. }