# 通用事件结构化表达-Json格式化日志
**通用事件结构化表达-Json格式化日志**模块继承于Python [`logging`](https://docs.python.org/3/library/logging.html) 模块,提供一种JSON格式输出网络安全事件日志。
网络空间安全事件表达模型详见“网络空间拟态防御领域通用日志表达标准规范.md”。
## 1. 功能特性
本模块继承Python logging模块的Formatters 与 FileHandler,提供JsonFormatter 与 JsonRotatingFileHandler。
**JsonFormatter:**实现日志的JSON格式化输出,其日志消息结构符合“网络空间拟态防御领域通用日志表达标准规范.md”。
**JsonRotatingFileHandler:**实现日志文件记录,其日志文件命名符合“网络空间拟态防御领域通用日志表达标准规范.md”。
## 2. API说明
本模块对外目前仅提供一个接口:
```python
def setup_logging(path: str, device_name: str, mode_name: str, log_type: str, log_level, skip_attrs:List[str]=[]):
"""
用于Logger object的生成,根据用户提供的配置,配置JsonFormatter与JsonRotatingFileHandler
:param path: 日志文件存放的目录路径
:param device_name: 需要记录日志的设备或系统名字
:param mode_name: 事件发生时所在的模块名
:param log_type: 日志类型,日志类型参见“网络空间拟态防御领域通用日志表达标准规范.md”
:param log_level: 参见python logging 中日志级别设置
:param skip_attrs: 需要在最终输出中隐藏的固定属性值
:return: 无
"""
```
可以设置隐藏的属性值:
```python
STATIC_ATTRS: Tuple[str, ...] = (
'filename', 'modulename', 'funcname', 'process', 'processName', 'thread', 'threadName', 'lineno',
)
```
## 3. demo
```python
from structured_log_json import jsonlogger
import logging
'''
1. inherited logging class Formatter;
2. inherited logging class Handler;
'''
def print_hi(name):
logger = jsonlogger.setup_logging("./", "mimicrouter", __name__, "dmf", logging.INFO,['filename','processName'])
test = {
"event_domain": "mimic_multimode_ruling",
"event_action": "attack",
"router_multimode_ruling":
[
{
"exception_type":"missing_router_item",
"action":"del_route",
"executor_role":"master",
"prefix":"2.2.2.2",
"mask":32,
"nexthop_info.nexthop":["100.0.13.3"],
"nexthop_info.ifname":["GigEth0"],
"nexthop_info.label":[]
},
{
"exception_type":"missing_router_item",
"action":"del_route",
"executor_role":"master",
"prefix":"11.11.11.11",
"mask":32,
"nexthop_info.nexthop":["100.0.13.3"],
"nexthop_info.ifname":["GigEth0"],
"nexthop_info.label":[]
},
{
"exception_type":"missing_router_item",
"action":"del_route",
"executor_role":"master",
"prefix":"100.0.12.0",
"mask":24,
"nexthop_info.nexthop":["100.0.13.3"],
"nexthop_info.ifname":["GigEth0"],
"nexthop_info.label":[]
},
{
"exception_type":"missing_router_item",
"action":"del_route",
"executor_role":"master",
"prefix":"100.0.17.0",
"mask":24,
"nexthop_info.nexthop":["100.0.13.3"]
}
]
}
for i in range(5):
#1. 带message附加信息方式,extra 是自定义字段
logger.info("test",extra=test)
#2. 直接传输自定义字段字典类型
logger.info(test)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
print_hi('PyCharm')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
```
### 4. How to install
1. [python 3.6+](https://www.python.org/downloads/) must be installed
2. Use pip to install module:
```python
python pip install structured_log_json
```
Raw data
{
"_id": null,
"home_page": "https://gitee.com/putty_git/structured_event_expression_in_-json_python.git",
"name": "structured-log-json",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6.9",
"maintainer_email": "",
"keywords": "",
"author": "Qiao.putty&&yangdunstc",
"author_email": "yangdunstc@163.com",
"download_url": "https://files.pythonhosted.org/packages/5c/58/2ce7d4addb36aec76079e12be3c34dbccf73a06110ed190c10b7ca53b736/structured_log_json-1.0.0.post2023.tar.gz",
"platform": "any",
"description": "# \u901a\u7528\u4e8b\u4ef6\u7ed3\u6784\u5316\u8868\u8fbe-Json\u683c\u5f0f\u5316\u65e5\u5fd7\r\n\r\n**\u901a\u7528\u4e8b\u4ef6\u7ed3\u6784\u5316\u8868\u8fbe-Json\u683c\u5f0f\u5316\u65e5\u5fd7**\u6a21\u5757\u7ee7\u627f\u4e8ePython [`logging`](https://docs.python.org/3/library/logging.html) \u6a21\u5757\uff0c\u63d0\u4f9b\u4e00\u79cdJSON\u683c\u5f0f\u8f93\u51fa\u7f51\u7edc\u5b89\u5168\u4e8b\u4ef6\u65e5\u5fd7\u3002\r\n\r\n\u7f51\u7edc\u7a7a\u95f4\u5b89\u5168\u4e8b\u4ef6\u8868\u8fbe\u6a21\u578b\u8be6\u89c1\u201c\u7f51\u7edc\u7a7a\u95f4\u62df\u6001\u9632\u5fa1\u9886\u57df\u901a\u7528\u65e5\u5fd7\u8868\u8fbe\u6807\u51c6\u89c4\u8303.md\u201d\u3002\r\n\r\n\r\n\r\n## 1. \u529f\u80fd\u7279\u6027\r\n\r\n\u672c\u6a21\u5757\u7ee7\u627fPython logging\u6a21\u5757\u7684Formatters \u4e0e FileHandler\uff0c\u63d0\u4f9bJsonFormatter \u4e0e JsonRotatingFileHandler\u3002\r\n\r\n**JsonFormatter\uff1a**\u5b9e\u73b0\u65e5\u5fd7\u7684JSON\u683c\u5f0f\u5316\u8f93\u51fa\uff0c\u5176\u65e5\u5fd7\u6d88\u606f\u7ed3\u6784\u7b26\u5408\u201c\u7f51\u7edc\u7a7a\u95f4\u62df\u6001\u9632\u5fa1\u9886\u57df\u901a\u7528\u65e5\u5fd7\u8868\u8fbe\u6807\u51c6\u89c4\u8303.md\u201d\u3002\r\n\r\n**JsonRotatingFileHandler\uff1a**\u5b9e\u73b0\u65e5\u5fd7\u6587\u4ef6\u8bb0\u5f55\uff0c\u5176\u65e5\u5fd7\u6587\u4ef6\u547d\u540d\u7b26\u5408\u201c\u7f51\u7edc\u7a7a\u95f4\u62df\u6001\u9632\u5fa1\u9886\u57df\u901a\u7528\u65e5\u5fd7\u8868\u8fbe\u6807\u51c6\u89c4\u8303.md\u201d\u3002\r\n\r\n## 2. API\u8bf4\u660e\r\n\r\n\u672c\u6a21\u5757\u5bf9\u5916\u76ee\u524d\u4ec5\u63d0\u4f9b\u4e00\u4e2a\u63a5\u53e3\uff1a\r\n\r\n```python\r\ndef setup_logging(path: str, device_name: str, mode_name: str, log_type: str, log_level, skip_attrs:List[str]=[]):\r\n \"\"\"\r\n \u7528\u4e8eLogger object\u7684\u751f\u6210\uff0c\u6839\u636e\u7528\u6237\u63d0\u4f9b\u7684\u914d\u7f6e\uff0c\u914d\u7f6eJsonFormatter\u4e0eJsonRotatingFileHandler\r\n :param path: \u65e5\u5fd7\u6587\u4ef6\u5b58\u653e\u7684\u76ee\u5f55\u8def\u5f84\r\n :param device_name: \u9700\u8981\u8bb0\u5f55\u65e5\u5fd7\u7684\u8bbe\u5907\u6216\u7cfb\u7edf\u540d\u5b57\r\n :param mode_name: \u4e8b\u4ef6\u53d1\u751f\u65f6\u6240\u5728\u7684\u6a21\u5757\u540d\r\n :param log_type: \u65e5\u5fd7\u7c7b\u578b\uff0c\u65e5\u5fd7\u7c7b\u578b\u53c2\u89c1\u201c\u7f51\u7edc\u7a7a\u95f4\u62df\u6001\u9632\u5fa1\u9886\u57df\u901a\u7528\u65e5\u5fd7\u8868\u8fbe\u6807\u51c6\u89c4\u8303.md\u201d\r\n :param log_level: \u53c2\u89c1python logging \u4e2d\u65e5\u5fd7\u7ea7\u522b\u8bbe\u7f6e\r\n :param skip_attrs: \u9700\u8981\u5728\u6700\u7ec8\u8f93\u51fa\u4e2d\u9690\u85cf\u7684\u56fa\u5b9a\u5c5e\u6027\u503c\r\n :return: \u65e0\r\n \"\"\"\r\n```\r\n\r\n\u53ef\u4ee5\u8bbe\u7f6e\u9690\u85cf\u7684\u5c5e\u6027\u503c\uff1a\r\n\r\n```python\r\nSTATIC_ATTRS: Tuple[str, ...] = (\r\n\t'filename', 'modulename', 'funcname', 'process', 'processName', 'thread', 'threadName', 'lineno',\r\n )\r\n```\r\n\r\n\r\n\r\n## 3. demo\r\n\r\n```python\r\nfrom structured_log_json import jsonlogger\r\nimport logging\r\n'''\r\n1. inherited logging class Formatter;\r\n2. inherited logging class Handler;\r\n'''\r\n\r\n\r\n\r\ndef print_hi(name):\r\n logger = jsonlogger.setup_logging(\"./\", \"mimicrouter\", __name__, \"dmf\", logging.INFO,['filename','processName'])\r\n test = {\r\n\r\n \"event_domain\": \"mimic_multimode_ruling\",\r\n \"event_action\": \"attack\",\r\n \"router_multimode_ruling\":\r\n [\r\n\t {\r\n \"exception_type\":\"missing_router_item\",\r\n \"action\":\"del_route\",\r\n \"executor_role\":\"master\",\r\n \"prefix\":\"2.2.2.2\",\r\n \"mask\":32,\r\n \"nexthop_info.nexthop\":[\"100.0.13.3\"],\r\n \"nexthop_info.ifname\":[\"GigEth0\"],\r\n \"nexthop_info.label\":[]\r\n },\r\n {\r\n \"exception_type\":\"missing_router_item\",\r\n \"action\":\"del_route\",\r\n \"executor_role\":\"master\",\r\n \"prefix\":\"11.11.11.11\",\r\n \"mask\":32,\r\n \"nexthop_info.nexthop\":[\"100.0.13.3\"],\r\n \"nexthop_info.ifname\":[\"GigEth0\"],\r\n \"nexthop_info.label\":[]\r\n },\r\n {\r\n \"exception_type\":\"missing_router_item\",\r\n \"action\":\"del_route\",\r\n \"executor_role\":\"master\",\r\n \"prefix\":\"100.0.12.0\",\r\n \"mask\":24,\r\n \"nexthop_info.nexthop\":[\"100.0.13.3\"],\r\n \"nexthop_info.ifname\":[\"GigEth0\"],\r\n \"nexthop_info.label\":[]\r\n },\r\n\t {\r\n \"exception_type\":\"missing_router_item\",\r\n \"action\":\"del_route\",\r\n \"executor_role\":\"master\",\r\n \"prefix\":\"100.0.17.0\",\r\n \"mask\":24,\r\n \"nexthop_info.nexthop\":[\"100.0.13.3\"]\r\n\t\t\t }\r\n\r\n\t\t ]\r\n }\r\n for i in range(5):\r\n #1. \u5e26message\u9644\u52a0\u4fe1\u606f\u65b9\u5f0f\uff0cextra \u662f\u81ea\u5b9a\u4e49\u5b57\u6bb5\r\n logger.info(\"test\",extra=test)\r\n #2. \u76f4\u63a5\u4f20\u8f93\u81ea\u5b9a\u4e49\u5b57\u6bb5\u5b57\u5178\u7c7b\u578b\r\n logger.info(test)\r\n \r\n\r\n# Press the green button in the gutter to run the script.\r\nif __name__ == '__main__':\r\n print_hi('PyCharm')\r\n\r\n# See PyCharm help at https://www.jetbrains.com/help/pycharm/\r\n\r\n```\r\n\r\n\r\n\r\n### 4. How to install\r\n\r\n1. [python 3.6+](https://www.python.org/downloads/) must be installed\r\n2. Use pip to install module:\r\n\r\n```python\r\npython pip install structured_log_json\r\n```\r\n\r\n",
"bugtrack_url": null,
"license": "LICENSE",
"summary": "Python structured event expression in json log.",
"version": "1.0.0.post2023",
"project_urls": {
"Homepage": "https://gitee.com/putty_git/structured_event_expression_in_-json_python.git"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5c582ce7d4addb36aec76079e12be3c34dbccf73a06110ed190c10b7ca53b736",
"md5": "7d295cd997b8230f62aa0c270250767a",
"sha256": "df3aaa9d8861fe307952dbedfdfb659b5ee208d1665f059f6c39418a9d8cf4a9"
},
"downloads": -1,
"filename": "structured_log_json-1.0.0.post2023.tar.gz",
"has_sig": false,
"md5_digest": "7d295cd997b8230f62aa0c270250767a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6.9",
"size": 15719,
"upload_time": "2023-10-07T09:56:58",
"upload_time_iso_8601": "2023-10-07T09:56:58.046290Z",
"url": "https://files.pythonhosted.org/packages/5c/58/2ce7d4addb36aec76079e12be3c34dbccf73a06110ed190c10b7ca53b736/structured_log_json-1.0.0.post2023.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-07 09:56:58",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "structured-log-json"
}