Skip to content

Commit

Permalink
Add ability to set log file and log level
Browse files Browse the repository at this point in the history
  • Loading branch information
mpchadwick committed Nov 4, 2020
1 parent abc35f7 commit b127ab1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ It is also possible to pass direct Faker function calls for [supported "raw prov

`dbanon` records messages about anything notable (e.g. invalid configuration) to the file `dbanon.log` in the directory from which you run it.

### `-log-file`

The `-log-file` flag can be used to have `dbanon` log to a different location.

```
mysqldump mydb | dbanon -config=myconfig.yml -log-file=var/dbanon.log
```

### `-log-level`

The `-log-level` flag can be used to control the verbosity of logs. Supported values can be found [here](https://github.com/sirupsen/logrus/blob/d131c24e23baaa812461202af6d7cfa388e2d292/logrus.go#L25-L45).

```
mysqldump mydb | dbanon -config=myconfig.yml -log-level=debug | gzip > mydb.sql.gz
```

The default log level is `info`.

### `-silent`

Logging can be disabled entirely by passing the `-silent` flag to `dbanon`

```
Expand Down
18 changes: 17 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"github.com/blang/semver"
"github.com/sirupsen/logrus"
"github.com/mpchadwick/dbanon/src"
"github.com/rhysd/go-github-selfupdate/selfupdate"
"io/ioutil"
Expand Down Expand Up @@ -37,6 +38,8 @@ func main() {
update := flag.Bool("update", false, "Auto update dbanon to the newest version")
ver := flag.Bool("version", false, "Get current version")
silent := flag.Bool("silent", false, "Disable all logging")
logFile := flag.String("log-file", "", "File to write logs to")
logLevel := flag.String("log-level", "", "Specify desired log level")

flag.Parse()

Expand All @@ -55,12 +58,25 @@ func main() {

dbanonLogger := dbanon.GetLogger()
if !*silent {
file, _ := os.OpenFile("dbanon.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
f := "dbanon.log"
if *logFile != "" {
f = *logFile
}
file, _ := os.OpenFile(f, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
dbanonLogger.SetOutput(file)
} else {
dbanonLogger.SetOutput(ioutil.Discard)
}

if *logLevel != "" {
level, err := logrus.ParseLevel(*logLevel)
if err != nil {
dbanonLogger.Error(err)
} else {
dbanonLogger.SetLevel(level)
}
}


config, err := dbanon.NewConfig(*requested)
if err != nil {
Expand Down

0 comments on commit b127ab1

Please sign in to comment.