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

Make option to choose place for logs storing #32

Open
lessfantasy opened this issue Aug 17, 2020 · 1 comment
Open

Make option to choose place for logs storing #32

lessfantasy opened this issue Aug 17, 2020 · 1 comment

Comments

@lessfantasy
Copy link

Now all logs store in syslog, that is not very useful. Please, make option to choose place for logs storing (file, syslog, work without logging) and make possible to set up log level (for ex. I don't want to store logs like ' clickhouse-bulk[40680]: 2020/08/17 13:58:01 INFO: send 0 rows to ...')

@ljluestc
Copy link


package main

import (
    "flag"
    "log"
    "os"
    "log/syslog"
)

// Define flags for log output and level
var (
    logOutput = flag.String("log-output", "syslog", "Log output destination (file, syslog, none)")
    logFile   = flag.String("log-file", "clickhouse-bulk.log", "Log file path (if log-output is file)")
    logLevel  = flag.String("log-level", "info", "Log level (debug, info, warn, error)")
)

func main() {
    flag.Parse()

    // Setup logger based on the output destination
    switch *logOutput {
    case "file":
        file, err := os.OpenFile(*logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
        if err != nil {
            log.Fatalf("Error opening log file: %v", err)
        }
        log.SetOutput(file)
    case "syslog":
        syslogWriter, err := syslog.New(syslog.LOG_NOTICE|syslog.LOG_DAEMON, "clickhouse-bulk")
        if err != nil {
            log.Fatalf("Error setting up syslog: %v", err)
        }
        log.SetOutput(syslogWriter)
    case "none":
        log.SetOutput(os.DevNull)
    default:
        log.Fatalf("Unsupported log output: %s", *logOutput)
    }

    // Example of setting up a simple log level check before logging
    // This is a basic implementation; for more complex scenarios consider using a logging library
    debugLog("This is a debug message")
    infoLog("This is an info message")
}

func debugLog(message string) {
    if *logLevel == "debug" {
        log.Println("DEBUG:", message)
    }
}

func infoLog(message string) {
    if *logLevel == "debug" || *logLevel == "info" {
        log.Println("INFO:", message)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants