修改Python文件日志输出位置


Python logging模块介绍:
http://blog.chinaunix.net/uid-26000296-id-4372063.html



[root@fuel ~]# vi /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy/models/node.py
     88
     89 class Node(Base):

    304     @classmethod
    305     def delete_by_ids(cls, ids):
    306         db.query(Node).filter(Node.id.in_(ids)).delete('fetch')


     38 from nailgun.logger import logger
可看出引用了:
/usr/lib/python2.6/site-packages/nailgun/logger.py中的logger变量。




[root@fuel ~]# vi /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/logger.py
     17 import logging
     18 import sys

     57 logger = make_nailgun_logger()

     31 def make_nailgun_logger():
     32     """Make logger for nailgun app writes logs to stdout
     33     """
     34     logger = logging.getLogger("nailgun")
     35     logger.setLevel(logging.DEBUG)
     36     handler = logging.StreamHandler(sys.stdout)
     37     handler.setFormatter(formatter)
     38     logger.addHandler(handler)
     39     return logger
     40

     28 formatter = logging.Formatter(LOGFORMAT, DATEFORMAT)

可见默认node.py将日志输出到标准输出,且最小日志输出级别是DEBUG级别。稍作改动,输出到日志文件中:
     36 #    handler = logging.StreamHandler(sys.stdout)
     37     handler = logging.FileHandler('/tmp/nailgun.log')



[root@fuel ~]# ls -l /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/logger.py*
-rwxr-xr-x 1 root root 3936 Jan 27 10:44 /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/logger.py
-rwxr-xr-x 2 root root 4509 Jun 19  2015 /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/logger.pyc
-rwxr-xr-x 2 root root 4509 Jun 19  2015 /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/logger.pyo



改完代码重启nailgun服务:
[root@fuel ~]# docker restart fuel-core-6.1-nailgun

验证日志已经生成:
[root@fuel ~]# ls -l /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/tmp/nailgun.log

[root@fuel ~]# ls -l /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/logger.py*
-rwxr-xr-x 1 root root 3936 Jan 27 10:44 /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/logger.py
-rw-r--r-- 1 root root 4509 Jan 27 10:49 /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/logger.pyc
-rwxr-xr-x 1 root root 4509 Jun 19  2015 /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/logger.pyo

制作符号链接:
[root@fuel ~]# ln -s /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/tmp/nailgun.log /root/nailgun.log



修改node.py源代码:
[root@fuel ~]# vi /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy/models/node.py
     89 class Node(Base):
     90     logger.logger.warning("____ffff____")

改完代码重启nailgun服务:
[root@fuel ~]# docker restart fuel-core-6.1-nailgun



验证代码被调用:
[root@fuel ~]# grep '____ffff____' nailgun.log
清空日志:
[root@fuel ~]# echo ''>nailgun.log
[root@fuel ~]# cat nailgun.log


[root@fuel ~]# fuel node
DEPRECATION WARNING: /etc/fuel/client/config.yaml exists and will be used as the source for settings. This behavior is deprecated. Please specify the path to your custom settings file in the FUELCLIENT_CUSTOM_SETTINGS environment variable.
id | status       | name  | cluster | ip           | mac               | roles                     | pending_roles | online | group_id
---|--------------|-------|---------|--------------|-------------------|---------------------------|---------------|--------|---------
10 | provisioning | RD530 | 1       | 168.5.21.106 | a0:36:9f:8d:40:88 | compute                   |               | True   | 1
2  | ready        | UCS2  | 1       | 168.5.21.101 | 00:25:b5:00:00:21 | cinder, controller, mongo |               | True   | 1
4  | ready        | R720  | 1       | 168.5.21.103 | c8:1f:66:d0:33:83 | compute                   |               | True   | 1
1  | ready        | UCS1  | 1       | 168.5.21.100 | 00:25:b5:00:00:11 | cinder, controller, mongo |               | True   | 1
3  | ready        | R420  | 1       | 168.5.21.102 | 00:1b:78:5a:9e:46 | cinder, controller, mongo |               | True   | 1
[root@fuel ~]# cat nailgun.log

[root@fuel ~]#
说明“fuel node”命令没调用到node.py。

posted @ 2016-01-27 11:21  Endoresu  阅读(3342)  评论(0编辑  收藏  举报