|
1 <?php |
|
2 /** |
|
3 * Licensed to the Apache Software Foundation (ASF) under one or more |
|
4 * contributor license agreements. See the NOTICE file distributed with |
|
5 * this work for additional information regarding copyright ownership. |
|
6 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
7 * (the "License"); you may not use this file except in compliance with |
|
8 * the License. You may obtain a copy of the License at |
|
9 * |
|
10 * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 * |
|
12 * Unless required by applicable law or agreed to in writing, software |
|
13 * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 * See the License for the specific language governing permissions and |
|
16 * limitations under the License. |
|
17 * |
|
18 * @package log4php |
|
19 */ |
|
20 |
|
21 /** |
|
22 * Returns the date/time of the logging request. |
|
23 * |
|
24 * Option: the datetime format, as used by the date() function. If |
|
25 * the option is not given, the default format 'c' will be used. |
|
26 * |
|
27 * There are several "special" values which can be given for this option: |
|
28 * 'ISO8601', 'ABSOLUTE' and 'DATE'. |
|
29 * |
|
30 * @package log4php |
|
31 * @subpackage pattern |
|
32 * @version $Revision: 1326626 $ |
|
33 * @since 2.3 |
|
34 */ |
|
35 class LoggerPatternConverterDate extends LoggerPatternConverter { |
|
36 |
|
37 const DATE_FORMAT_ISO8601 = 'c'; |
|
38 |
|
39 const DATE_FORMAT_ABSOLUTE = 'H:i:s'; |
|
40 |
|
41 const DATE_FORMAT_DATE = 'd M Y H:i:s.u'; |
|
42 |
|
43 private $format = self::DATE_FORMAT_ISO8601; |
|
44 |
|
45 private $specials = array( |
|
46 'ISO8601' => self::DATE_FORMAT_ISO8601, |
|
47 'ABSOLUTE' => self::DATE_FORMAT_ABSOLUTE, |
|
48 'DATE' => self::DATE_FORMAT_DATE, |
|
49 ); |
|
50 |
|
51 private $useLocalDate = false; |
|
52 |
|
53 public function activateOptions() { |
|
54 |
|
55 // Parse the option (date format) |
|
56 if (!empty($this->option)) { |
|
57 if (isset($this->specials[$this->option])) { |
|
58 $this->format = $this->specials[$this->option]; |
|
59 } else { |
|
60 $this->format = $this->option; |
|
61 } |
|
62 } |
|
63 |
|
64 // Check whether the pattern contains milliseconds (u) |
|
65 if (preg_match('/(?<!\\\\)u/', $this->format)) { |
|
66 $this->useLocalDate = true; |
|
67 } |
|
68 } |
|
69 |
|
70 public function convert(LoggerLoggingEvent $event) { |
|
71 if ($this->useLocalDate) { |
|
72 return $this->date($this->format, $event->getTimeStamp()); |
|
73 } |
|
74 return date($this->format, $event->getTimeStamp()); |
|
75 } |
|
76 |
|
77 /** |
|
78 * Currently, PHP date() function always returns zeros for milliseconds (u) |
|
79 * on Windows. This is a replacement function for date() which correctly |
|
80 * displays milliseconds on all platforms. |
|
81 * |
|
82 * It is slower than PHP date() so it should only be used if necessary. |
|
83 */ |
|
84 private function date($format, $utimestamp) { |
|
85 $timestamp = floor($utimestamp); |
|
86 $ms = floor(($utimestamp - $timestamp) * 1000); |
|
87 $ms = str_pad($ms, 3, '0', STR_PAD_LEFT); |
|
88 |
|
89 return date(preg_replace('`(?<!\\\\)u`', $ms, $format), $timestamp); |
|
90 } |
|
91 } |