-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.idea | ||
DS_Store | ||
*.pyc | ||
*.log | ||
*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# gjhandler | ||
|
||
gjhandler is a simple log handler which generates JSON files for Cloud Logging and google-fluentd. | ||
|
||
## Example | ||
|
||
```python | ||
import logging | ||
import gjhandler | ||
|
||
logger = logging.getLogger("gj_logger") | ||
logger.setLevel(logging.DEBUG) | ||
logger.addHandler(gjhandler.GoogleJsonHandler(filename="log_test.json")) | ||
|
||
logger.info("this is info") | ||
logger.error("this is error") | ||
``` | ||
|
||
### Resulting Outputs | ||
|
||
```json | ||
{"timestamp": {"seconds": 1472657255, "nanos": 154423952}, "message": "this is info", "severity": "INFO", "thread": 140735257141248} | ||
{"timestamp": {"seconds": 1472657255, "nanos": 155013084}, "message": "this is error", "severity": "ERROR", "thread": 140735257141248} | ||
``` | ||
|
||
## Setting for google-fluentd | ||
|
||
```xml | ||
<source> | ||
type tail | ||
format json | ||
path /var/log/python/*.log,/var/log/python/*.json | ||
read_from_head true | ||
tag python | ||
</source> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import json | ||
import logging.handlers | ||
import math | ||
|
||
|
||
class GoogleJsonHandler(logging.handlers.RotatingFileHandler): | ||
|
||
def __init__(self, filename): | ||
super(GoogleJsonHandler, self).__init__( | ||
filename, | ||
maxBytes=8*1024*1024, | ||
backupCount=5 | ||
) | ||
|
||
def format(self, record): | ||
message = super(GoogleJsonHandler, self).format(record) | ||
subsecond, second = math.modf(record.created) | ||
payload = { | ||
"message": message, | ||
"timestamp": { | ||
"seconds": int(second), | ||
"nanos": int(subsecond * 1e9) | ||
}, | ||
"thread": record.thread, | ||
"severity": record.levelname, | ||
} | ||
return json.dumps(payload, ensure_ascii=False) |