Enhancing error log

I have a component that is shared and used in multiple places in my webapp.

In the event log, I can spot some error but I can’t figure out the context and which page is generating this error so that I can fix it.

Is there a way I could add the URL generating the error in the log by any chance ?

Hi @chris,

Yes, that’s possible. Since OctoberCMS runs on top of Laravel, you can include the request context when writing to the log. For example:

\Log::error($e->getMessage(), [
    'url'    => request()->fullUrl(),
    'method' => request()->method(),
    'ip'     => request()->ip(),
]);

If you want it globally (so every log entry automatically contains the URL), you can add something like this in your plugin’s boot() method:

public function boot()
{
    if (!app()->runningInConsole()) {
        $request = request();

        \Log::shareContext([
            'url'    => $request->fullUrl(),
            'method' => $request->method(),
            'ip'     => $request->ip(),
        ]);
    }
}

That way, whenever the error happens — no matter which page — the system log will include the URL that triggered it.

1 Like

brilliant, I didnt know about this shareContext method, thanks a lot @apinard

1 Like