Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
蜕变的菜鸟
glances
提交
e9da136f
G
glances
项目概览
蜕变的菜鸟
/
glances
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glances
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e9da136f
编写于
10月 22, 2016
作者:
N
nicolargo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make the logger configurable (issue #900)
上级
9374e0c9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
142 addition
and
47 deletion
+142
-47
NEWS
NEWS
+1
-0
docs/config.rst
docs/config.rst
+73
-0
glances/__init__.py
glances/__init__.py
+2
-0
glances/logger.py
glances/logger.py
+66
-47
未找到文件。
NEWS
浏览文件 @
e9da136f
...
...
@@ -14,6 +14,7 @@ Enhancements and news features:
* Docker alerts and actions (issue #875)
* Glances API returns the processes PPID (issue #926)
* Configure server cached time from the command line --cached-time (issue #901)
* Make the log logger configurable (issue #900)
Bugs corrected:
...
...
docs/config.rst
浏览文件 @
e9da136f
...
...
@@ -110,3 +110,76 @@ By default, the ``glances.log`` file is under the temporary directory:
If ``glances.log`` is not writable, a new file will be created and
returned to the user console.
If you want to use another system path or change the log message, you can use
your own logger configuration. First of all you have to create a glances.json
file with (for example) the following content (JSON format):
.. code-block:: json
{
"version": 1,
"disable_existing_loggers": "False",
"root": {
"level": "INFO",
"handlers": ["file", "console"]
},
"formatters": {
"standard": {
"format": "%(asctime)s -- %(levelname)s -- %(message)s"
},
"short": {
"format": "%(levelname)s: %(message)s"
},
"free": {
"format": "%(message)s"
}
},
"handlers": {
"file": {
"level": "DEBUG",
"class": "logging.handlers.RotatingFileHandler",
"formatter": "standard",
"filename": "/var/tmp/glances.log"
},
"console": {
"level": "CRITICAL",
"class": "logging.StreamHandler",
"formatter": "free"
}
},
"loggers": {
"debug": {
"handlers": ["file", "console"],
"level": "DEBUG"
},
"verbose": {
"handlers": ["file", "console"],
"level": "INFO"
},
"standard": {
"handlers": ["file"],
"level": "INFO"
},
"requests": {
"handlers": ["file", "console"],
"level": "ERROR"
},
"elasticsearch": {
"handlers": ["file", "console"],
"level": "ERROR"
},
"elasticsearch.trace": {
"handlers": ["file", "console"],
"level": "ERROR"
}
}
}
and start Glances using the following command line:
.. code-block:: console
LOG_CFG=<path>/glances.json glances
Note: Replace <path> by the folder where your glances.json file is hosted.
glances/__init__.py
浏览文件 @
e9da136f
...
...
@@ -20,6 +20,7 @@
"""Init the Glances software."""
# Import system libs
import
locale
import
platform
import
signal
...
...
@@ -43,6 +44,7 @@ except ImportError:
from
glances.logger
import
logger
from
glances.main
import
GlancesMain
# Check locale
try
:
locale
.
setlocale
(
locale
.
LC_ALL
,
''
)
except
locale
.
Error
:
...
...
glances/logger.py
浏览文件 @
e9da136f
...
...
@@ -22,66 +22,66 @@
import
logging
import
os
import
tempfile
import
json
from
logging.config
import
dictConfig
# Define the logging configuration
LOGGING_CFG
=
{
'version'
:
1
,
'disable_existing_loggers'
:
False
,
'root'
:
{
'level'
:
'INFO'
,
'handlers'
:
[
'file'
,
'console'
]
"version"
:
1
,
"disable_existing_loggers"
:
"False"
,
"root"
:
{
"level"
:
"INFO"
,
"handlers"
:
[
"file"
,
"console"
]
},
'formatters'
:
{
'standard'
:
{
'format'
:
'%(asctime)s -- %(levelname)s -- %(message)s'
"formatters"
:
{
"standard"
:
{
"format"
:
"%(asctime)s -- %(levelname)s -- %(message)s"
},
'short'
:
{
'format'
:
'%(levelname)s: %(message)s'
"short"
:
{
"format"
:
"%(levelname)s: %(message)s"
},
'free'
:
{
'format'
:
'%(message)s'
"free"
:
{
"format"
:
"%(message)s"
}
},
'handlers'
:
{
'file'
:
{
'level'
:
'DEBUG'
,
'class'
:
'logging.handlers.RotatingFileHandler'
,
'formatter'
:
'standard'
,
# http://stackoverflow.com/questions/847850/cross-platform-way-of-getting-temp-directory-in-python
"handlers"
:
{
"file"
:
{
"level"
:
"DEBUG"
,
"class"
:
"logging.handlers.RotatingFileHandler"
,
"formatter"
:
"standard"
,
'filename'
:
os
.
path
.
join
(
tempfile
.
gettempdir
(),
'glances.log'
)
},
'console'
:
{
'level'
:
'CRITICAL'
,
'class'
:
'logging.StreamHandler'
,
'formatter'
:
'free'
"console"
:
{
"level"
:
"CRITICAL"
,
"class"
:
"logging.StreamHandler"
,
"formatter"
:
"free"
}
},
'loggers'
:
{
'debug'
:
{
'handlers'
:
[
'file'
,
'console'
],
'level'
:
'DEBUG'
,
"loggers"
:
{
"debug"
:
{
"handlers"
:
[
"file"
,
"console"
],
"level"
:
"DEBUG"
},
'verbose'
:
{
'handlers'
:
[
'file'
,
'console'
],
'level'
:
'INFO'
"verbose"
:
{
"handlers"
:
[
"file"
,
"console"
],
"level"
:
"INFO"
},
'standard'
:
{
'handlers'
:
[
'file'
],
'level'
:
'INFO'
"standard"
:
{
"handlers"
:
[
"file"
],
"level"
:
"INFO"
},
'requests'
:
{
'handlers'
:
[
'file'
,
'console'
],
'level'
:
'ERROR'
,
"requests"
:
{
"handlers"
:
[
"file"
,
"console"
],
"level"
:
"ERROR"
},
'elasticsearch'
:
{
'handlers'
:
[
'file'
,
'console'
],
'level'
:
'ERROR'
,
},
'elasticsearch.trace'
:
{
'handlers'
:
[
'file'
,
'console'
],
'level'
:
'ERROR'
,
"elasticsearch"
:
{
"handlers"
:
[
"file"
,
"console"
],
"level"
:
"ERROR"
},
"elasticsearch.trace"
:
{
"handlers"
:
[
"file"
,
"console"
],
"level"
:
"ERROR"
}
}
}
...
...
@@ -98,12 +98,31 @@ def tempfile_name():
return
ret
def
glances_logger
():
"""Build and return the logger."""
temp_path
=
tempfile_name
()
def
glances_logger
(
env_key
=
'LOG_CFG'
):
"""Build and return the logger.
env_key define the env var where a path to a specific JSON logger
could be defined
:return: logger -- Logger instance
"""
_logger
=
logging
.
getLogger
()
LOGGING_CFG
[
'handlers'
][
'file'
][
'filename'
]
=
temp_path
dictConfig
(
LOGGING_CFG
)
# Overwrite the default logger file
LOGGING_CFG
[
'handlers'
][
'file'
][
'filename'
]
=
tempfile_name
()
# By default, use the LOGGING_CFG lgger configuration
config
=
LOGGING_CFG
# Check if a specific configuration is available
user_file
=
os
.
getenv
(
env_key
,
None
)
if
user_file
and
os
.
path
.
exists
(
user_file
):
# A user file as been defined. Use it...
with
open
(
user_file
,
'rt'
)
as
f
:
config
=
json
.
load
(
f
)
# Load the configuration
dictConfig
(
config
)
return
_logger
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录