1: <?php
2: /**
3: * Fluent-PHP-Logger
4: *
5: * Copyright (C) 2011 Shuhei Tanuma
6: *
7: * Licensed under the Apache License, Version 2.0 (the "License");
8: * you may not use this file except in compliance with the License.
9: * You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
13: * Unless required by applicable law or agreed to in writing, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19: namespace Fluent\Logger;
20:
21: /**
22: * Console Logger
23: *
24: * Console Logger client outputs readable log message to specifeid file handle.
25: *
26: * @author Shuhei Tanuma <stanuma@zynga.com>
27: */
28: class ConsoleLogger extends BaseLogger
29: {
30: protected $tag;
31: protected $handle;
32:
33: /**
34: * create Console logger object.
35: *
36: * @param string $tag primary tag
37: * @param resource $handle
38: * @return ConsoleLogger
39: */
40: public function __construct($tag,$handle)
41: {
42: $this->tag = $tag;
43: $this->handle = $handle;
44: }
45:
46: /**
47: * Fluent singleton API.
48: *
49: * @todo fixed singleton api.
50: * @param string $tag primary tag
51: * @param resource $handle
52: * @return FluentLogger created logger object.
53: */
54: public static function open($tag, $handle)
55: {
56: $logger = new self($tag,$handle);
57: //\Fluent\Logger::$current = $logger;
58: return $logger;
59: }
60:
61: /**
62: * send a message to specified fluentd.
63: *
64: * @param mixied $data
65: */
66: public function post($data, $additional = null)
67: {
68: $params = array();
69: $tag = $this->tag;
70: if (!empty($additional)) {
71: $tag .= ".{$additional}";
72: }
73:
74: foreach ($data as $key => $value) {
75: $params[$key] = $value;
76: }
77:
78: $time = new \DateTime("@".time(),new \DateTimeZone(date_default_timezone_get()));
79: $result = sprintf("%s %s: %s\n",$time->format("Y-m-d H:i:s O"), $tag, json_encode($params));
80: fwrite($this->handle,$result);
81: }
82: }
83: