guangie88 / spdlog_setup

spdlog setup initialization via file configuration for convenience.
MIT License
77 stars 41 forks source link

Where to store config file #35

Open nj2901 opened 5 years ago

nj2901 commented 5 years ago

Hi, its a wonderful work here but i was trying to execute the examples given and getting setup error .

Can you please help that where should i be saving my conf.toml file ? I am saving it in the same folder as of my script. but i am getting exception of setup_Error

guangie88 commented 5 years ago

There is no restriction as to where you should be saving your TOML config for spdlog_setup, it works the same way as you would do ifstream to open file based on your pwd. You can first try with ifstream to open the file and read out the file string content, and if it works, spdlog_setup::from_file will work with the same given path.

To try to provide additional help, assuming you have your working directory at /path/to/your/dir and your compiled progam is named yourprogram, and your program follows the example to read like this spdlog_setup::from_file("log_conf.toml");, you should have your CMD/bash to be be in /path/to/your/dir first, then execute your program with ./yourprogram (or yourprogram.exe if CMD).

If you are still unable to get it working, you should clarify how where are your executable and config file are located.

nj2901 commented 5 years ago

No i put it in the same directory (my code and the config file) but i am getting setup error :

#include "spdlog_setup/conf.h"

#include <iostream>
#include <string>

int main() {
    try {
        // spdlog_setup::setup_error thrown if file not found
        spdlog_setup::from_file("log_conf.toml");

        // assumes that root logger has been initialized

        std::cout<<"Printing data\n"<<std::flush;
        auto logger = spdlog::get("root");
        logger->trace("trace message");
        logger->debug("debug message");
        logger->info("info message");
        logger->warn("warn message");
        logger->error("error message");
        logger->critical("critical message");
        logger->info("Print data");
        std::cout<<"Printing data\n"<<std::flush;

        // ...
    } catch (const spdlog_setup::setup_error &) {
        std::cout<<"Printing data in 1st catch\n"<<std::flush; // this statement is printing for me
        // ...
    } catch (const std::exception &) {
        std::cout<<"Printing data in 2nd catch\n"<<std::flush;
        // ...
    }
}

I am compiling as below :

clang++ -o staticConfigFileSetup staticConfigFileSetup.cpp

and my config file resides in the same path as of my code.

Moreover,

You can first try with ifstream to open the file and read out the file string content, and if it works, 

i tried this and it works fine.

guangie88 commented 5 years ago

Okay then in that case, I will assume that you have your binary + config TOML file + you are running your command with relative path in the same dir as the binary.

I will need you to do these two things before I can help:

  1. Show the content log_conf.toml that you are using that gives the error.
  2. Do the following to show what is the error message in setup_error:
    } catch (const spdlog_setup::setup_error &e) {
    std::cout << e.what() << "\n";
    } ...
nj2901 commented 5 years ago

e.what()

Printing data in 1st catch
Sink 'syslog_st' error:
 > Invalid sink type 'syslog_sink_st' found

log_conf.toml


# level is optional for both sinks and loggers
# level for error logging is 'err', not 'error'
# _st => single threaded, _mt => multi threaded
# syslog_sink is automatically thread-safe by default, no need for _mt suffix

# max_size supports suffix
# - T (terabyte)
# - G (gigabyte)
# - M (megabyte)
# - K (kilobyte)
# - or simply no suffix (byte)

# check out https: // github.com/gabime/spdlog/wiki/3.-Custom-formatting
global_pattern = "[%Y-%m-%dT%T%z] [%L] <%n>: %v"

[[sink]]
name = "console_st"
type = "stdout_sink_st"

[[sink]]
name = "console_mt"
type = "stdout_sink_mt"

[[sink]]
name = "color_console_st"
type = "color_stdout_sink_st"

[[sink]]
name = "color_console_mt"
type = "color_stdout_sink_mt"

[[sink]]
name = "file_out"
type = "basic_file_sink_st"
filename = "log/spdlog_setup.log"
# truncate field is optional
# truncate = false (default)
level = "info"
# optional flag to indicate the set - up to create the log dir first
create_parent_dir = true

[[sink]]
name = "file_err"
type = "basic_file_sink_mt"
filename = "log/spdlog_setup_err.log"
truncate = true
level = "err"
# to show that create_parent_dir is indeed optional(defaults to false)

[[sink]]
name = "rotate_out"
type = "rotating_file_sink_st"
base_filename = "log/rotate_spdlog_setup.log"
max_size = "1M"
max_files = 10
level = "info"

[[sink]]
name = "rotate_err"
type = "rotating_file_sink_mt"
base_filename = "log/rotate_spdlog_setup_err.log"
max_size = "1M"
max_files = 10
level = "err"

[[sink]]
name = "daily_out"
type = "daily_file_sink_st"
base_filename = "log/daily_spdlog_setup.log"
rotation_hour = 17
rotation_minute = 30
level = "info"

[[sink]]
name = "daily_err"
type = "daily_file_sink_mt"
base_filename = "log/daily_spdlog_setup_err.log"
rotation_hour = 17
rotation_minute = 30
level = "err"

[[sink]]
name = "null_sink_st"
type = "null_sink_st"

[[sink]]
name = "null_sink_mt"
type = "null_sink_mt"

# only works for Linux
[[sink]]
name = "syslog_st"
type = "syslog_sink_st"
# generally no need to fill up the optional fields below
# ident = "" (default)
# syslog_option = 0 (default)
# syslog_facility = LOG_USER (default macro value)

# only works for Linux
[[sink]]
name = "syslog_mt"
type = "syslog_sink_mt"
# generally no need to fill up the optional fields below
# ident = "" (default)
# syslog_option = 0 (default)
# syslog_facility = LOG_USER (default macro value)

[[pattern]]
name = "succient"
value = "%c-%L: %v"

[[logger]]
name = "root"
sinks = [
    "color_console_mt",
    "rotate_out", "rotate_err"]
level = "trace"

[[logger]]
name = "console"
sinks = ["console_st", "console_mt"]
pattern = "succient"
guangie88 commented 5 years ago

I will be assuming that you are on Linux, since syslog is not supported on Windows (OSX should be fine I think?)

Based on the error message: "Invalid sink type 'syslog_sink_st' found", the most likely reason is that your spdlog_setup is not from the latest tag v0.3.0-alpha.2 (or latest commit, whichever is fine).

There was a breaking change between v0.3.0-alpha.1 and v0.3.0-alpha.2; v0.3.0-alpha.1 only supports pre-v1.X spdlog and uses syslog_sink instead of syslog_sink_st (see: https://github.com/guangie88/spdlog_setup/tree/v0.3.0-alpha.1#static-file-configuration, and search for syslog_sink), while v0.3.0-alpha.2 uses both syslog_sink_st and syslog_sink_mt (which follows the same example as the current master README.md). So in order to fix the problem, do make sure you are using either the latest commit of spdlog_setup, or use v0.3.0-alpha.2.

In any case, you might not need syslog sink (or the many other types of sink in the example) to begin with, so it will also work if you just remove syslog sinks from your TOML config.

Hope this helps to make your set-up work.