-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonDataFormatter.php
83 lines (76 loc) · 2.38 KB
/
CommonDataFormatter.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
<?php
namespace phpWTL;
use phpWTL\aBasicDataFormatter;
require_once 'aBasicDataFormatter.php';
/**
* Data formatter for the common log format.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.2.1
* @api
*/
class CommonDataFormatter extends aBasicDataFormatter {
protected static $loggerContent= null;
protected static $fieldDescriptor= null;
/**
* @param object $loggerContent Provide a LoggerContent object. Also store the format blueprint via the LoggerContent object (which knows its FormatDescriptor).
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.1
*/
protected function __construct($loggerContent= null) {
static::$loggerContent= $loggerContent;
if (static::$loggerContent) static::$fieldDescriptor= $loggerContent->getFormatDescriptor();
}
/**
* Format only a single log field and store it in the associated LoggerContent object.
*
* @param string $field_name ID of log format field.
* @param string $value Provide an (optional) value to format and pass thru to the LoggerContent object, so allowing for the injection of external data.
*
* @author Michael Beyer <mgbeyer@gmx.de>
* @version v0.1.2
* @api
*/
public function formatField($field_name, $value= null) {
if (static::$fieldDescriptor && static::$loggerContent) {
$format_string= static::$fieldDescriptor->getFormatter($field_name);
if (!$value) $value= static::$loggerContent->__get($field_name);
switch ($field_name) {
case "host_ip":
break;
case "client_identity":
break;
case "user_id":
break;
case "timestamp":
// zone info from strftime is not really reliable in terms of format (at least under Win)
// so if it is wanted let's get rid of it and build our own ;)
$o= date('Z')/3600;
$o= sprintf("%+03d", $o);
$zone_offset= str_pad($o, 5, "0", STR_PAD_RIGHT);
$format_string= preg_replace("/%z|%Z/", $zone_offset, $format_string);
$value= strftime($format_string, FormatDescriptorHelper::datetimeString2timestamp($value));
break;
case "request_line":
break;
case "status_code":
break;
case "content_size":
if ($value=="0") {
switch($format_string) {
case "%b":
$value= "-";
break;
case "%B":
$value= "0";
break;
}
}
break;
}
static::$loggerContent->__set($field_name, $value);
}
}
}
?>