Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.89% covered (warning)
57.89%
11 / 19
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Logging
57.89% covered (warning)
57.89%
11 / 19
40.00% covered (danger)
40.00%
2 / 5
31.80
0.00% covered (danger)
0.00%
0 / 1
 __construct
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
2.21
 path
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 filename
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 filepath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 mkPath
50.00% covered (danger)
50.00%
4 / 8
0.00% covered (danger)
0.00%
0 / 1
22.50
1<?php
2namespace DiegoSorrentino\Library;
3
4/**
5 * Logging class for PHP scripts
6 * Stores log file for a process in "<basePath>/processName/YYYY-MM-DD.log"
7 * if prefixDir is not set, try to store in "./".
8 * if the procedure is unable to create "<basePath>/processName" directory, fallback to /dev/stderr
9 * 
10 * @param $processName     string set filename for log file. Default value 'file.log'. It will be $path/$filename
11 * @param $basePath string (nullable) set basePath to store log file. Default value './log/' (if not exists it will be created)
12 */
13class Logging{
14    public const fallbackFilePath = '/dev/stderr';
15
16    private string $path;
17    private string $filename;
18    private string $filepath;
19
20    public function __construct( string $processName = 'process/', string $basePath = './'){
21
22        $this->path = sprintf("%s/%s", $basePath, $processName) ;
23        $this->filename = sprintf("%s.log", date('Y-m-d'));
24
25        if( ! $this->mkPath($this->path) ){
26            $this->filepath = self::fallbackFilePath;
27            ini_set ( 'error_log', $this->filepath);
28            return;
29        }
30
31        $this->filepath = sprintf('%s/%s', $this->path, $this->filename);
32
33        ini_set ( 'error_log', $this->filepath);
34    }
35
36    /**
37     * Path of current log dir
38     * 
39     * @return string Path of current log dir
40     */
41    public function path():string { return $this->path; }
42
43    /**
44     * Filename of current log file
45     * 
46     * @return string Filename of current log file
47     */
48    public function filename():string { return $this->filename; }
49
50    /**
51     * Full filepath of current log file
52     * 
53     * @return string Full filepath of current log file
54     */
55    public function filepath():string { return $this->filepath; }
56
57    /**
58     * Create path dir for log files
59     * Check if file exists AND is directory AND is writable, or try to create/set it permission
60     * 
61     * @param string $path Lock path to try to create 
62     *
63     * @return bool True if file exists AND is directory AND is writable, false otherwise
64     */
65    private function mkPath(string $path):bool{
66        if( file_exists($path) && is_dir($path) && is_writable($path) )
67            return true;
68 
69        if( file_exists($path) && is_dir($path) && ! is_writable($path) )
70            return chmod($path, 0755);
71        
72        if( file_exists($path) && ! is_dir($path) ){
73            if ( ! unlink($path) )
74                return false; //and below i try to mkdir
75        }
76
77        //file not exists
78        return mkdir($path, 0755, true);
79    }
80}
81
82?>