1: <?php
2: /**
3: * Fluent-Logger-PHP
4: *
5: * Copyright (C) 2011 - 2012 Fluent-Logger-PHP Contributors
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: */
27: class ConsoleLogger extends BaseLogger
28: {
29: protected $tag;
30: protected $handle;
31:
32: /**
33: * create Console logger object.
34: *
35: * @param resource $handle
36: * @return ConsoleLogger
37: */
38: public function __construct($handle)
39: {
40: $this->handle = $handle;
41: }
42:
43: /**
44: * fluent-logger compatible API.
45: *
46: * @param resource $handle
47: * @return FluentLogger created logger object.
48: */
49: public static function open($handle)
50: {
51: $logger = new self($handle);
52: return $logger;
53: }
54:
55: /**
56: * send a message to specified fluentd.
57: *
58: * @param string $tag
59: * @param array $data
60: */
61: public function post($tag ,array $data)
62: {
63: $entity = new Entity($tag,$data);
64:
65: return $this->write(sprintf("%s\t%s\t%s\n",
66: date(\DateTime::ISO8601,$entity->getTime()),
67: $entity->getTag(),
68: json_encode($entity->getData())
69: ));
70: }
71:
72: /**
73: * @param Entity $entity
74: * @return bool
75: */
76: public function post2(Entity $entity)
77: {
78: return $this->write(sprintf("%s\t%s\t%s\n",
79: date(\DateTime::ISO8601,$entity->getTime()),
80: $entity->getTag(),
81: json_encode($entity->getData())
82: ));
83: }
84:
85: protected function write($buffer)
86: {
87: return fwrite($this->handle, $buffer);
88: }
89: }
90: