-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.php
113 lines (101 loc) · 3.76 KB
/
Log.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php namespace Mreschke\Helpers;
use Mreschke\Helpers\Date;
use Mreschke\Helpers\File;
/**
* Mreschke style logging
* @copyright 2014 Matthew Reschke
* @license http://mreschke.com/license/mit
* @author Matthew Reschke <mail@mreschke.com>
*/
class Log implements LogInterface
{
private $logFile;
private $format;
/**
* Create a new Log instance
* @param string $logFile path to log
* @param string $format
*/
public function __construct($logFile, $format = 'readable')
{
$this->logFile = $logFile;
$this->format = $format;
}
/**
* Write one line of log data
* @param string $data
* @param string $summary
* @param string $type
* @param string $action
* @return void
*/
public function write($data, $summary = 'Main', $type = 'log', $action = 'next')
{
/*
Date | Type | Action | Summary | Log Description
------------------------|------------|------------|-----------------|----------------
2013-12-02 19:00:00.859 | ########## | ########## | Main | ESP3Generator Started at 12/2/
2013-12-02 19:00:01.000 | Log | Next | Mode | Running in resume mode
2013-12-02 19:00:01.078 | Log | Next | StartGen | Processing each enabled, unpro
2013-12-02 19:00:01.093 | Log | Done | Finished | ESP3Generator Finished (comple
*/
#Output Separator
$os = "\n";
if ($this->format == 'readable') {
$os = "\r\n";
}
$date = Date::date('Y-m-d H:i:s.u');
# Define Type
$type = trim(strtolower($type));
if (strlen($type) > 10) {
$type = substr($type, 0, 10);
}
$types = array('critical', 'error', 'expected', 'log', 'unexpected', 'unusual', '##########', '==========', '----------', '++++++++++');
if (in_array($type, $types)) {
$type = ucfirst($type);
} else {
$type = "Log";
}
#Define Action
$action = trim(strtolower($action));
if (strlen($action) > 10) {
$action = substr($action, 0, 10);
}
$actions = array('done', 'halt', 'next', 'skip', '##########', '==========', '----------', '++++++++++');
if (in_array($action, $actions)) {
$action = ucfirst($action);
} else {
$action = "Next";
}
#Define Summary
$summary = trim($summary);
if (strlen($summary) > 15) {
$summary = substr($summary, 0, 15);
}
# Create initial File
if (!file_exists($this->logFile)) {
touch($this->logFile);
#NO, I don't want these headers
#if ($this->format == 'readable') {
# \Snippets\File::append($this->logFile, "Date | Type | Action | Summary | Log Description$os");
# \Snippets\File::append($this->logFile, "------------------------|------------|------------|-----------------|----------------$os");
# \Snippets\File::append($this->logFile, "$os");
#}
}
if (file_exists($this->logFile)) {
if ($this->format == 'readable') {
if ($data) {
$output = "$date | ";
$output .= str_pad($type, 10).' | ';
$output .= str_pad($action, 10).' | ';
$output .= str_pad($summary, 15).' | ';
$output .= $data;
$output .= $os;
} else {
$output = $data.$os;
}
File::append($this->logFile, $output);
}
}
}
}