1
0
mirror of https://github.com/nxshock/gron.git synced 2024-11-27 03:41:00 +05:00

Try to add MS Sql driver logging

This commit is contained in:
nxshock 2022-05-11 18:59:33 +05:00
parent c08aadda4f
commit a1ed8d73a0
3 changed files with 22 additions and 5 deletions

8
job.go
View File

@ -14,6 +14,7 @@ import (
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
formatter "github.com/antonfisher/nested-logrus-formatter" formatter "github.com/antonfisher/nested-logrus-formatter"
mssql "github.com/denisenkom/go-mssqldb"
log "github.com/sirupsen/logrus" 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, _ = 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") jobLogFile.WriteString("\n")
logWriter := io.MultiWriter(logFile, jobLogFile) logWriter := io.MultiWriter(mainLogFile, jobLogFile)
log := log.New() log := log.New()
log.SetFormatter(&formatter.Formatter{ log.SetFormatter(&formatter.Formatter{
@ -201,6 +202,11 @@ func (j *Job) runSql(jobLogFile *os.File) error {
} }
defer db.Close() defer db.Close()
msSqlDriver, ok := db.Driver().(*mssql.Driver)
if ok {
msSqlDriver.SetLogger(&logFile{jobLogFile})
}
_, err = db.Exec(j.JobConfig.SqlText) _, err = db.Exec(j.JobConfig.SqlText)
if err != nil { if err != nil {
return err return err

15
log.go
View File

@ -1,14 +1,25 @@
package main package main
import ( import (
"fmt"
"os" "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 { func initLogFile() error {
var err 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 return err
} }

View File

@ -36,7 +36,7 @@ func init() {
TrimMessages: true}) TrimMessages: true})
//multiWriter := io.MultiWriter(os.Stderr, logFile) //multiWriter := io.MultiWriter(os.Stderr, logFile)
//log.SetOutput(multiWriter) //log.SetOutput(multiWriter)
log.SetOutput(logFile) log.SetOutput(mainLogFile)
log.SetLevel(log.InfoLevel) log.SetLevel(log.InfoLevel)
initTemplate() initTemplate()
@ -103,7 +103,7 @@ func main() {
log.Info("Got stop signal.") log.Info("Got stop signal.")
err = logFile.Close() err = mainLogFile.Close()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }