Some checks failed
Release / Build & Release (push) Has been cancelled
Comprehensive release containing structural, UX, and behavioral upgrades since v0.1.0: 1. Namespace Transition: Renamed core executable and project namespaces from 'smart-monitor' to 'smart-shutdown'. 2. Objective Vocabulary Refactoring: Normalized output strings and logging descriptors system-wide to a strict, professional tone. 3. Advanced Status Query: 'status' subcommand now retrieves the parsed configuration, log locations/sizes, and tails the last 10 lines of the local log file. 4. Runtime Configuration Setter: Introduced 'config set' subcommand to modify the configuration file with strict type validations. 5. Auto-System Deployment: Remapped 'install' to clone the executable into system domains and register global PATH variables. 6. Cleaner Removal: 'uninstall' purges binary clones and clears environmental variables, leaving zero traces. 7. Documentation Rewrite: Generated an objective README file featuring copy-paste ready Markdown blocks.
90 lines
2.0 KiB
Go
90 lines
2.0 KiB
Go
package logger
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/fatih/color"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
"smart-shutdown/pkg/config"
|
|
)
|
|
|
|
var fileLogger *lumberjack.Logger
|
|
|
|
func InitLogger() error {
|
|
logDir := config.GetLogDir()
|
|
if err := os.MkdirAll(logDir, 0755); err != nil {
|
|
logDir = "logs"
|
|
os.MkdirAll(logDir, 0755)
|
|
}
|
|
|
|
logFile := filepath.Join(logDir, "network_monitor.log")
|
|
|
|
fileLogger = &lumberjack.Logger{
|
|
Filename: logFile,
|
|
MaxSize: 10,
|
|
MaxBackups: 30,
|
|
MaxAge: 30,
|
|
Compress: false,
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func writeLog(level, plainPrefix, format string, v ...interface{}) {
|
|
msg := fmt.Sprintf(format, v...)
|
|
timestamp := time.Now().Format("2006/01/02 15:04:05")
|
|
|
|
if fileLogger != nil {
|
|
plainLine := fmt.Sprintf("%s %s %s\n", plainPrefix, timestamp, msg)
|
|
fileLogger.Write([]byte(plainLine))
|
|
}
|
|
|
|
prefixColorFunc := getPrefixColor(level)
|
|
coloredPrefix := prefixColorFunc(plainPrefix)
|
|
coloredLine := fmt.Sprintf("%s %s %s\n", coloredPrefix, timestamp, msg)
|
|
|
|
if level == "FAIL" || level == "CRITICAL" {
|
|
fmt.Fprint(os.Stderr, coloredLine)
|
|
} else {
|
|
fmt.Fprint(os.Stdout, coloredLine)
|
|
}
|
|
}
|
|
|
|
func getPrefixColor(level string) func(a ...interface{}) string {
|
|
switch level {
|
|
case "SUCCESS":
|
|
return color.New(color.FgGreen).SprintFunc()
|
|
case "WARN":
|
|
return color.New(color.FgYellow).SprintFunc()
|
|
case "FAIL":
|
|
return color.New(color.FgRed).SprintFunc()
|
|
case "CRITICAL":
|
|
return color.New(color.FgHiRed).SprintFunc()
|
|
default:
|
|
return color.New(color.Reset).SprintFunc()
|
|
}
|
|
}
|
|
|
|
func Info(format string, v ...interface{}) {
|
|
writeLog("INFO", "[INFO]", format, v...)
|
|
}
|
|
|
|
func Succ(format string, v ...interface{}) {
|
|
writeLog("SUCCESS", "[SUCCESS]", format, v...)
|
|
}
|
|
|
|
func Warn(format string, v ...interface{}) {
|
|
writeLog("WARN", "[WARN]", format, v...)
|
|
}
|
|
|
|
func Fail(format string, v ...interface{}) {
|
|
writeLog("FAIL", "[FAIL]", format, v...)
|
|
}
|
|
|
|
func Crit(format string, v ...interface{}) {
|
|
writeLog("CRITICAL", "[CRITICAL]", format, v...)
|
|
}
|