From a1ed8d73a02f56dab6634432a8fcb670b3f3e469 Mon Sep 17 00:00:00 2001 From: nxshock Date: Wed, 11 May 2022 18:59:33 +0500 Subject: [PATCH] Try to add MS Sql driver logging --- job.go | 8 +++++++- log.go | 15 +++++++++++++-- main.go | 4 ++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/job.go b/job.go index 410ad2b..d7c5af7 100644 --- a/job.go +++ b/job.go @@ -14,6 +14,7 @@ import ( "github.com/BurntSushi/toml" formatter "github.com/antonfisher/nested-logrus-formatter" + mssql "github.com/denisenkom/go-mssqldb" log "github.com/sirupsen/logrus" ) @@ -99,7 +100,7 @@ func (j *Job) openAndMergeLog() (logEntry *log.Entry, jobLogFile *os.File) { jobLogFile, _ = os.OpenFile(filepath.Join(config.LogFilesPath, j.Name+".log"), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) // TODO: handle error jobLogFile.WriteString("\n") - logWriter := io.MultiWriter(logFile, jobLogFile) + logWriter := io.MultiWriter(mainLogFile, jobLogFile) log := log.New() log.SetFormatter(&formatter.Formatter{ @@ -201,6 +202,11 @@ func (j *Job) runSql(jobLogFile *os.File) error { } defer db.Close() + msSqlDriver, ok := db.Driver().(*mssql.Driver) + if ok { + msSqlDriver.SetLogger(&logFile{jobLogFile}) + } + _, err = db.Exec(j.JobConfig.SqlText) if err != nil { return err diff --git a/log.go b/log.go index dbf23bf..7e13ec5 100644 --- a/log.go +++ b/log.go @@ -1,14 +1,25 @@ package main import ( + "fmt" "os" ) -var logFile *os.File +type logFile struct{ file *os.File } + +func (lf *logFile) Printf(format string, v ...interface{}) { + fmt.Fprintf(lf.file, format, v...) +} + +func (lf *logFile) Println(v ...interface{}) { + fmt.Fprintln(lf.file, v...) +} + +var mainLogFile *os.File func initLogFile() error { var err error - logFile, err = os.OpenFile(config.LogFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) + mainLogFile, err = os.OpenFile(config.LogFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) return err } diff --git a/main.go b/main.go index 82335bb..9832fc5 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,7 @@ func init() { TrimMessages: true}) //multiWriter := io.MultiWriter(os.Stderr, logFile) //log.SetOutput(multiWriter) - log.SetOutput(logFile) + log.SetOutput(mainLogFile) log.SetLevel(log.InfoLevel) initTemplate() @@ -103,7 +103,7 @@ func main() { log.Info("Got stop signal.") - err = logFile.Close() + err = mainLogFile.Close() if err != nil { log.Fatal(err) }