Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Base.php
Go to the documentation of this file.
1 <?php
8 
10 use Monolog\Formatter\LineFormatter;
11 use Monolog\Handler\StreamHandler;
12 use Monolog\Logger;
13 
14 class Base extends StreamHandler
15 {
19  protected $fileName;
20 
24  protected $loggerType = Logger::DEBUG;
25 
29  protected $filesystem;
30 
36  public function __construct(
38  $filePath = null,
39  $fileName = null
40  ) {
41  $this->filesystem = $filesystem;
42  if (!empty($fileName)) {
43  $this->fileName = $this->sanitizeFileName($fileName);
44  }
45  parent::__construct(
46  $filePath ? $filePath . $this->fileName : BP . DIRECTORY_SEPARATOR . $this->fileName,
47  $this->loggerType
48  );
49 
50  $this->setFormatter(new LineFormatter(null, null, true));
51  }
52 
59  private function sanitizeFileName($fileName)
60  {
61  if (!is_string($fileName)) {
62  throw new \InvalidArgumentException('Filename expected to be a string');
63  }
64 
65  $parts = explode('/', $fileName);
66  $parts = array_filter($parts, function ($value) {
67  return !in_array($value, ['', '.', '..']);
68  });
69 
70  return implode('/', $parts);
71  }
72 
80  public function write(array $record)
81  {
82  $logDir = $this->filesystem->getParentDirectory($this->url);
83  if (!$this->filesystem->isDirectory($logDir)) {
84  $this->filesystem->createDirectory($logDir);
85  }
86 
87  parent::write($record);
88  }
89 }
__construct(DriverInterface $filesystem, $filePath=null, $fileName=null)
Definition: Base.php:36
$value
Definition: gender.phtml:16
const BP
Definition: autoload.php:14