Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logging with levels. #51

Merged
merged 7 commits into from Oct 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions copy.go
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"

"github.com/DGKSK8LIFE/redisql/utils"
"github.com/DGKSK8LIFE/redisql/utils/logging"
_ "github.com/go-sql-driver/mysql"
"gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -42,6 +43,7 @@ func NewConfig(filePath string) (*Config, error) {

// CopyToString reads a desired SQL table's rows and writes them to Redis strings
func (c Config) CopyToString() error {
logging.Log("Starting CopyToString", logging.One)
if err := utils.Convert("string", c.SQLUser, c.SQLPassword, c.SQLDatabase, c.SQLHost, c.SQLPort, c.SQLTable, c.RedisAddr, c.RedisPass, c.SQLType); err != nil {
return err
}
Expand All @@ -50,6 +52,7 @@ func (c Config) CopyToString() error {

// CopyToList reads a desired SQL table's rows and writes them to Redis lists
func (c Config) CopyToList() error {
logging.Log("Starting CopyToList", logging.One)
if err := utils.Convert("list", c.SQLUser, c.SQLPassword, c.SQLDatabase, c.SQLHost, c.SQLPort, c.SQLTable, c.RedisAddr, c.RedisPass, c.SQLType); err != nil {
return err
}
Expand All @@ -58,6 +61,7 @@ func (c Config) CopyToList() error {

// CopyToHash reads a desired SQL table's rows and writes them to Redis hashes
func (c Config) CopyToHash() error {
logging.Log("Starting CopyToHash", logging.One)
if err := utils.Convert("hash", c.SQLUser, c.SQLPassword, c.SQLDatabase, c.SQLHost, c.SQLPort, c.SQLTable, c.RedisAddr, c.RedisPass, c.SQLType); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions redisql/main.go
Expand Up @@ -5,6 +5,7 @@ import (
"os"

redisql "github.com/DGKSK8LIFE/redisql"
"github.com/DGKSK8LIFE/redisql/utils/logging"
)

var dataType *string
Expand All @@ -18,6 +19,9 @@ func init() {
}

func main() {
logging.InitLogging(logging.Three)
//utils.SetLogFile("log.txt")

config, err := redisql.NewConfig(*file)
if err != nil {
panic(err)
Expand Down
4 changes: 4 additions & 0 deletions utils/db.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/go-redis/redis/v8"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/DGKSK8LIFE/redisql/utils/logging"
)

// CTX is the global context for Redis
Expand Down Expand Up @@ -63,11 +64,13 @@ func Convert(redisType, sqlUser, sqlPassword, sqlDatabase, sqlHost, sqlPort, sql

switch sqlType {
case "mysql":
logging.Log("Opening MySQL", logging.One)
db, err = OpenMySQL(sqlUser, sqlPassword, sqlDatabase, sqlHost, sqlPort)
if err != nil {
return err
}
case "postgres":
logging.Log("Opening PostgreSQL", logging.One)
db, err = OpenPostgres(sqlUser, sqlPassword, sqlDatabase, sqlHost, sqlPort)
if err != nil {
return err
Expand Down Expand Up @@ -151,5 +154,6 @@ func Convert(redisType, sqlUser, sqlPassword, sqlDatabase, sqlHost, sqlPort, sql
return err
}
}
logging.Log("Copying done", logging.One)
return nil
}
57 changes: 57 additions & 0 deletions utils/logging/logging.go
@@ -0,0 +1,57 @@
package logging

import (
"log"
"os"
)

type Level int32

// Logging level. One means show only a little. Three means show all levels.
const (
One = 1
Two = 2
Three = 3
)

var logLevelIsSet bool = false
var logFileIsSet bool = false
var loggingLevel Level

func SetLogFile(filepath string) {
if logFileIsSet {
panic("Cannot change logfile midway!")
}

f, err := os.OpenFile(filepath, os.O_RDWR | os.O_CREATE, 0666)
if err != nil {
panic(err)
}

logFileIsSet = true
log.SetOutput(f)
}

func InitLogging(level Level) {
if logLevelIsSet {
panic("Logging level has already been set! It cannot be changed midway.")
} else if level < 1 || level > 3 {
panic("Logging level must be between 1 and 3!")
}

logLevelIsSet = true
loggingLevel = level
}


func Log(s string, level Level) {
if level < 1 || level > 3 {
panic("Logging level must be between 1 and 3!")
} else if level <= loggingLevel {
log.Printf("Verbosity:%d | %s\n", level, s)
}
}