# Log filename and line

**URL:** <https://discourse.cakephp.org/t/log-filename-and-line/4611>\
**Category:** Need Help\
**Created:** [July 23, 2018, 3:25pm UTC](https://discourse.cakephp.org/t/log-filename-and-line/4611 "2018-07-23T15:25:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![okelet](https://avatars.discourse-cdn.com/v4/letter/o/919ad9/32.png) [@okelet](https://discourse.cakephp.org/u/okelet)\
**Post date:** [July 23, 2018, 3:25pm UTC](https://discourse.cakephp.org/t/log-filename-and-line/4611/1 "2018-07-23T15:25:49Z")

</div>

Hi, using Cakephp 3.6, is there any way, with minimal modifications, to log the file and line (and perhaps class and function) where the log message was fired, like log4j does?

---

<div class="post-metadata">

**Author:** ![okelet](https://avatars.discourse-cdn.com/v4/letter/o/919ad9/32.png) [@okelet](https://discourse.cakephp.org/u/okelet)\
**Post date:** [October 26, 2018, 11:09am UTC](https://discourse.cakephp.org/t/log-filename-and-line/4611/2 "2018-10-26T11:09:12Z")

</div>

Up? Searched a lot without finding any solution… 😞

---

<div class="post-metadata">

**Author:** ![MarceliSzpak](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@MarceliSzpak](https://discourse.cakephp.org/u/MarceliSzpak)\
**Post date:** [October 26, 2018, 12:58pm UTC](https://discourse.cakephp.org/t/log-filename-and-line/4611/3 "2018-10-26T12:58:55Z")

</div>

![](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/7/72c21aee023e044610631593935a4f45ca005873.gif)

[https://book.cakephp.org/3.0/en/core-libraries/logging.html#creating-log-adapters](https://book.cakephp.org/3.0/en/core-libraries/logging.html#creating-log-adapters)  
+  
[http://php.net/manual/en/function.debug-backtrace.php](http://php.net/manual/en/function.debug-backtrace.php)

Proof of concept :

[https://pastebin.com/294W1hjw](https://pastebin.com/294W1hjw)

---

<div class="post-metadata">

**Author:** ![okelet](https://avatars.discourse-cdn.com/v4/letter/o/919ad9/32.png) [@okelet](https://discourse.cakephp.org/u/okelet)\
**Post date:** [October 26, 2018, 10:30pm UTC](https://discourse.cakephp.org/t/log-filename-and-line/4611/4 "2018-10-26T22:30:50Z")

</div>

Sorry, I won’t ask more… If this is useful for anyone; create a file inside `src\Log\Engine\MyLog.php`:

```
<?php

namespace App\Log\Engine;

use App\ArrayUtils;
use Cake\Log\Engine\FileLog;
use Nette\Utils\Strings;

class MyLog extends FileLog
{
    public function log($level, $message, array $context = [])
    {
        $cakeHome = realpath(dirname(realpath( __FILE__ )) . DS . ".." . DS . ".." . DS . "..");
        $stacks = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 4);
        $stack = end($stacks);
        $file = ArrayUtils::getString($stack, "file");
        $relFile = $file;
        if (Strings::startsWith($file, $cakeHome . DS)) {
            $relFile = Strings::substring($file, strlen($cakeHome)+1);
        }
        $class = ArrayUtils::getString($stack, "class");
        $function = ArrayUtils::getString($stack, "function");
        $line = ArrayUtils::getInt($stack, "line");
        parent::log($level, "{$class}::{$function}::{$line} - {$message}", $context);
    }
}

```

`ArrayUtils` is just a helper to extract data from an array. Then, configure Cake to use this logger in `app.php`:

```
'error' => [
    'className' => 'App\Log\Engine\MyLog',
    'path' => LOGS,
    'file' => 'error',
    'url' => env('LOG_ERROR_URL', null),
    'scopes' => false,
    'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
],
```
