• Resolved ccprog

    (@ccprog)


    I am currently hooking up a plugin to log events with Simple History, but there are several problems. I was following your tutorial on custom loggers and came up with this:

    function crw_simple_history_logger ( $simpleHistory ) {
        class crw_logger extends SimpleLogger {
    
            public $slug = __CLASS__;
    
            function getInfo() {
                $arr_info = array(
                    "name" => __( 'Crosswordsearch submissions', 'crosswordsearch' ),
                    "description" => __('User submitted solutions for crosswordsearch riddles', 'crosswordsearch' )
                );
                return $arr_info;
            }
    
            function loaded() {
                // action hook to my plugin
                add_action( 'crw_solution_submitted', function  ( $user, $submission ) {
                    $text = crw_log_text( $user, $submission );
                    SimpleLogger()->info( implode( '<br/>', $text ), array(
                        '_user_id' => $user->ID,
                        '_initiator' => SimpleLoggerLogInitiators::WP_USER
                    ) );
                }, 10, 2 );
    
            }
        }
    
        $simpleHistory->register_logger('crw_logger');
    };
    add_action("simple_history/add_custom_logger", 'crw_simple_history_logger' );'

    Now,

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author P?r Thernstr?m

    (@eskapism)

    Hi!

    > Why does the logger field still show up as “SimpleLogger”?

    The code looks good and just needs a small modification.

    You log the event like this:

    SimpleLogger()->info( ... );

    but for simple history to know that your logger is the one logging the event you need to call it like this:

    $this->info( ... );

    so you call the info() method on your own logger class, that then uses the info() method from the SimpleLogger class, since it’s extended from it.

    > How do I get the logger to show up in the filter list?

    You need to add a new “labels” key to the info-array. I need to write another blog post describing this. However I think you can just search for “crw_logger” now in the “containing words”-field to get all your logged events.

    > How can I log messages containing simple markup

    For security reasons html-tags will not be outputed in the log. To make your logged events be more rich you can add a method called getLogRowPlainTextOutput() or a method called getLogRowDetailsOutput(). Using these methods you can override the default, simple, log format. You can check the included loggers for details on how to use this.

    Thread Starter ccprog

    (@ccprog)

    SimpleLogger()->info was quoted verbatim from your blog post. You should correct that.

    Plugin Author P?r Thernstr?m

    (@eskapism)

    Oh, thanks for pointing out that mistake! I will correct the blogpost.

    Plugin Author P?r Thernstr?m

    (@eskapism)

    Now the example on the blog is updated to correctly use $this when logging:

    https://simple-history.com/2015/create-logger/

    If you still have any issues please let know.

    Thread Starter ccprog

    (@ccprog)

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom Logger not working as expected’ is closed to new replies.