From c29f0b2cd749c7307450b91c389bf28cbe694944 Mon Sep 17 00:00:00 2001 From: SunBK201 Date: Fri, 15 Nov 2024 01:19:22 +0800 Subject: [PATCH] refactor: refactor logging setup and directory structure for ua3f service --- install.sh | 2 +- openwrt/files/luci/cbi.lua | 2 +- openwrt/files/ua3f.init | 3 ++- opkg/etc/init.d/ua3f | 3 ++- opkg/usr/lib/lua/luci/model/cbi/ua3f.lua | 2 +- src/go.mod | 1 + src/go.sum | 2 ++ src/log/log.go | 13 +++++++++---- 8 files changed, 19 insertions(+), 9 deletions(-) diff --git a/install.sh b/install.sh index 0fcf870..9ff6701 100755 --- a/install.sh +++ b/install.sh @@ -31,7 +31,7 @@ ck_ua3f_log() { if [ -f "/var/log/ua3f.log" ]; then rm "/var/log/ua3f.log" fi - touch /var/log/ua3f.log && chmod 666 /var/log/ua3f.log + mkdir -p /var/log/ua3f && chmod ugo+w /var/log/ua3f } dl_ua3f() { diff --git a/openwrt/files/luci/cbi.lua b/openwrt/files/luci/cbi.lua index 591f39d..ea879f2 100644 --- a/openwrt/files/luci/cbi.lua +++ b/openwrt/files/luci/cbi.lua @@ -44,7 +44,7 @@ log_level:value("panic") log = main:taboption("log", TextValue, "") log.readonly = true log.cfgvalue = function(self, section) - return luci.sys.exec("cat /var/log/ua3f.log") + return luci.sys.exec("cat /var/log/ua3f/ua3f.log") end log.rows = 30 diff --git a/openwrt/files/ua3f.init b/openwrt/files/ua3f.init index c961050..29cf225 100755 --- a/openwrt/files/ua3f.init +++ b/openwrt/files/ua3f.init @@ -29,7 +29,8 @@ start_service() { config_get_bool partial_replace "main" "partial_replace" 0 config_get log_level "main" "log_level" "info" - chmod o+w /var/log + mkdir -p /var/log/ua3f + chmod o+w /var/log/ua3f procd_open_instance "$NAME" procd_set_param command "$PROG" diff --git a/opkg/etc/init.d/ua3f b/opkg/etc/init.d/ua3f index c961050..29cf225 100755 --- a/opkg/etc/init.d/ua3f +++ b/opkg/etc/init.d/ua3f @@ -29,7 +29,8 @@ start_service() { config_get_bool partial_replace "main" "partial_replace" 0 config_get log_level "main" "log_level" "info" - chmod o+w /var/log + mkdir -p /var/log/ua3f + chmod o+w /var/log/ua3f procd_open_instance "$NAME" procd_set_param command "$PROG" diff --git a/opkg/usr/lib/lua/luci/model/cbi/ua3f.lua b/opkg/usr/lib/lua/luci/model/cbi/ua3f.lua index 009840a..0686201 100755 --- a/opkg/usr/lib/lua/luci/model/cbi/ua3f.lua +++ b/opkg/usr/lib/lua/luci/model/cbi/ua3f.lua @@ -44,7 +44,7 @@ log_level:value("panic") log = main:taboption("log", TextValue, "") log.readonly = true log.cfgvalue = function(self, section) - return luci.sys.exec("cat /var/log/ua3f.log") + return luci.sys.exec("cat /var/log/ua3f/ua3f.log") end log.rows = 30 diff --git a/src/go.mod b/src/go.mod index ac2652f..cd588bf 100644 --- a/src/go.mod +++ b/src/go.mod @@ -6,6 +6,7 @@ require ( github.com/dlclark/regexp2 v1.11.4 github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/sirupsen/logrus v1.9.3 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) require ( diff --git a/src/go.sum b/src/go.sum index 3a831f1..9920d77 100644 --- a/src/go.sum +++ b/src/go.sum @@ -21,6 +21,8 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/log/log.go b/src/log/log.go index ccff8c4..0c62a66 100644 --- a/src/log/log.go +++ b/src/log/log.go @@ -4,11 +4,11 @@ import ( "bytes" "fmt" "io" - "log" "os" "strings" "github.com/sirupsen/logrus" + "gopkg.in/natefinch/lumberjack.v2" ) type uctFormatter struct { @@ -27,11 +27,16 @@ func (formatter *uctFormatter) Format(entry *logrus.Entry) ([]byte, error) { } func SetLogConf(level string) { + log_file := "/var/log/ua3f/ua3f.log" writer1 := &bytes.Buffer{} writer2 := os.Stdout - writer3, err := os.OpenFile("/var/log/ua3f.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755) - if err != nil { - log.Fatalf("create file ua3f.log failed: %v", err) + writer3 := &lumberjack.Logger{ + Filename: log_file, + MaxSize: 2, // megabytes + MaxBackups: 3, + MaxAge: 28, //days + LocalTime: true, + Compress: true, } logrus.SetOutput(io.MultiWriter(writer1, writer2, writer3)) formatter := &uctFormatter{}