logger.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 //File, empty if LogToFile is false
  21. }
  22. // Create a default logger
  23. func NewLogger(logFilePrefix string, logFolder string, logToFile bool) (*Logger, error) {
  24. if logToFile {
  25. err := os.MkdirAll(logFolder, 0775)
  26. if err != nil {
  27. return nil, err
  28. }
  29. }
  30. thisLogger := Logger{
  31. LogToFile: logToFile,
  32. Prefix: logFilePrefix,
  33. LogFolder: logFolder,
  34. }
  35. if logToFile {
  36. logFilePath := thisLogger.getLogFilepath()
  37. f, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755)
  38. if err != nil {
  39. return nil, err
  40. }
  41. thisLogger.CurrentLogFile = logFilePath
  42. thisLogger.file = f
  43. }
  44. return &thisLogger, nil
  45. }
  46. // Create a non-persistent logger for one-time uses
  47. func NewTmpLogger() (*Logger, error) {
  48. return NewLogger("", "", false)
  49. }
  50. func (l *Logger) getLogFilepath() string {
  51. year, month, _ := time.Now().Date()
  52. return filepath.Join(l.LogFolder, l.Prefix+"_"+strconv.Itoa(year)+"-"+strconv.Itoa(int(month))+".log")
  53. }
  54. // PrintAndLog will log the message to file and print the log to STDOUT
  55. func (l *Logger) PrintAndLog(title string, message string, originalError error) {
  56. go func() {
  57. l.Log(title, message, originalError)
  58. }()
  59. log.Println("[" + title + "] " + message)
  60. }
  61. func (l *Logger) Log(title string, errorMessage string, originalError error) {
  62. if l.LogToFile {
  63. l.ValidateAndUpdateLogFilepath()
  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. }