1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19: namespace Fluent\Logger;
20:
21: 22: 23:
24: class FileLogger extends BaseLogger
25: {
26: const MAX_WRITE_RETRY = 10;
27:
28: protected $tag;
29: protected $path;
30:
31: 32: 33: 34: 35: 36:
37: public function __construct($path)
38: {
39: $this->path = $path;
40: $fp = @fopen($path, "c");
41:
42: if (is_resource($fp)) {
43: $this->fp = $fp;
44: } else {
45: throw new \RuntimeException("could not open file {$path}");
46: }
47: }
48:
49: 50: 51: 52: 53:
54: public static function open($path)
55: {
56: $logger = new self($path);
57: return $logger;
58: }
59:
60: 61: 62: 63: 64: 65:
66: public function post($tag,array $data)
67: {
68: $entity = new Entity($tag, $data);
69: return $this->postImpl($entity);
70: }
71:
72: 73: 74: 75: 76:
77: public function post2(Entity $entity)
78: {
79: return $this->postImpl($entity);
80: }
81:
82: protected function postImpl(Entity $entity)
83: {
84: $packed = json_encode($entity->getData());
85: $data = $wbuffer = sprintf("%s\t%s\t%s\n",
86: date(\DateTime::ISO8601,
87: $entity->getTime()),
88: $entity->getTag(),
89: $packed . PHP_EOL
90: );
91:
92: $length = strlen($data);
93: $written = 0;
94:
95: try {
96: if (!flock($this->fp, LOCK_EX)) {
97: throw new \Exception('could not obtain LOCK_EX');
98: }
99: fseek($this->fp,-1, SEEK_END);
100:
101: while ($written < $length) {
102: $nwrite = fwrite($this->fp, $wbuffer);
103: if ($nwrite === false) {
104: throw new \Exception("could not write message");
105: } else if ($nwrite === "") {
106: throw new \Exception("connection aborted");
107: } else if ($nwrite === 0) {
108: if ($retry > self::MAX_WRITE_RETRY) {
109: throw new \Exception("failed fwrite retry: max retry count");
110: }
111: $retry++;
112: }
113: $written += $nwrite;
114: $wbuffer = substr($wbuffer,$written);
115: }
116:
117: flock($this->fp, LOCK_UN);
118: } catch (\Exception $e) {
119: $this->processError($this, $entity, $e->getMessage());
120: return false;
121: }
122: return true;
123: }
124: }
125: