Menu

boost::log child class attribute/tag when calling parent function

mzazi
2017-08-28
2017-08-28
  • mzazi

    mzazi - 2017-08-28

    Hi Andrey,
    I'm implementing a logger in parent class and child classes and would like to utilize some kind of tag or attribute that prints "calling object's" class name. Here is an example:

    class some_parent
    {
        some_parent() { init_with_class_tag ("parent_class");
        func_1() { LOG_INFO ("function 1 logging something");
        func_2() { LOG_WARN ("function 2 logging something");
    };
    
    class some_child : public some_parent
    {
        some_child() { init_with_class_tag ("child_class");
        func_3() { LOG_ERROR ("function 3 logging something");
        func_4() { LOG_INFO ("function 4 logging something");
    }
    
    main ()
    {
        some_parent pr_1;
        pr_1.func_2();
    
        some_child ch_1;
        ch_1.func_1();
        ch_1.func_2();
        ch_1.func_3();
        ch_1.func_4();
    }
    

    Desired output is something like:
    07:15:00 WARNING parent_class - function 2 logging something
    07:15:01 INFO child_class - function 1 logging something
    07:15:02 WARNING child_class - function 2 logging something
    07:15:03 ERROR child_class - function 3 logging something
    07:15:04 INFO child_class - function 4 logging something

    Current output is something like:
    07:15:00 WARNING parent_class - function 2 logging something
    07:15:01 INFO parent_class - function 1 logging something
    07:15:02 WARNING parent_class - function 2 logging something
    07:15:03 ERROR child_class - function 3 logging something
    07:15:04 INFO child_class - function 4 logging something

    I would like child object's tag/attribute to be logged when it calls parent class's function, even if I don't override parent functions. I have tried to use attributes (member_logger_var.add_attribute("ClassName", ....), I have also tried using Channel filtering. All with no luck, my child object calls still calls parent function which includes parent tag/attribute.

    I'm open to trying anything to achieve this functionality. Thank you

     
  • Andrey Semashev

    Andrey Semashev - 2017-08-28

    I suppose, you could use separate loggers in the parent and the child class' methods. Those loggers would provide the class' tag.

     
  • mzazi

    mzazi - 2017-08-28

    I'm not sure if you meant to say to create separate loggers for each object. If so, then I tried that.

    But if you really meant create a new logger in each of class's methods, I suppose I can give it a try. Just wondering if this will become resource intensive to create a logger in each method of derived class.

    I'll let you know if it works, thanks!

     
  • Alex

    Alex - 2017-08-28

    Andrey, if we use separate loggers, each of them has to be separately instantiated that can create a huge overhead if I am dealing with lots of instances of objects (orders, quotes, trades, etc...). Is there a way to associate a tag with each class instead? In other words, is there a way to minimize the overhead?

     
  • Andrey Semashev

    Andrey Semashev - 2017-08-28

    I'm not sure if you meant to say to create separate loggers for each object.

    Yes, I meant to suggest to make a logger a class member - one for the parent class and another one for the child class. Each class' methods would use the respective logger and produce log records marked with the appropriate tag.

    Is there a way to associate a tag with each class instead?

    Sure, you can make the logger a static member of the class.

     

Log in to post a comment.