示意代码如下:

#_*_coding:UTF-8_*_

import time
import socket
import os
import sys if sys.version_info.major == 2:
reload(sys)
sys.setdefaultencoding('utf8') class LogLevel(object):
debug = 'DEBUG'
info = 'INFO'
warning = 'WARN'
error = 'ERROR'
fatal = 'FATAL' class Log4P(object):
def __init__(self, *args_arr, **args_dict):
self.module_name = "unknown"
self.task_id = "unknown"
if "module_name" in args_dict:
self.module_name = args_dict["module_name"]
if "task_id" in args_dict:
self.task_id = str(args_dict["task_id"])
self.host_name = socket.gethostname() def debug(self, *message):
return self.log(*message, level=LogLevel.debug) def info(self, *message):
return self.log(*message, level=LogLevel.info) def warn(self, *message):
return self.log(*message, level=LogLevel.warning) def error(self, *message):
return self.log(*message, level=LogLevel.error) def fatal(self, *message):
return self.log(*message, level=LogLevel.fatal) def static(self, case):
if case == 1:
info = "run success"
elif case == 2:
info = "run failed"
elif case == 3:
info = "no master"
elif case == 4:
info = "error master"
elif case == 5:
info = "run fail before switch master"
else:
info = "unknown error"
return self.info(info) if sys.version_info.major == 2:
def log(self, message, level=LogLevel.info):
curr_time = time.localtime(time.time())
log_context = format('[%d-%02d-%02d %02d:%02d:%02d][%s][%s][%s-%s]' \
% (curr_time.tm_year, curr_time.tm_mon, curr_time.tm_mday,
curr_time.tm_hour, curr_time.tm_min, curr_time.tm_sec,
str(level), self.host_name, self.module_name, self.task_id))
print(log_context, message, os.linesep)
else:
def log(self, *message, level=LogLevel.info):
curr_time = time.localtime(time.time())
log_context = format('[%d-%02d-%02d %02d:%02d:%02d][%s][%s][%s-%s]' \
% (curr_time.tm_year, curr_time.tm_mon, curr_time.tm_mday,
curr_time.tm_hour, curr_time.tm_min, curr_time.tm_sec,
str(level), self.host_name, self.module_name, self.task_id))
print(log_context, *message, os.linesep) if __name__ == "__main__":
logger = Log4P(module_name="dga", task_id="dga_id")
logger.log("hello world")
logger.log("hello world", "this is a test")
a = 100
logger.log("hello world", "var:", a)

  

python 中根据python版本(2或3)定义函数的更多相关文章

  1. python python中那些双下划线开头的那些函数都是干啥用用的

    1.写在前面 今天遇到了__slots__,,所以我就想了解下python中那些双下划线开头的那些函数都是干啥用用的,翻到了下面这篇博客,看着很全面,我只了解其中的一部分,还不敢乱下定义. 其实如果足 ...

  2. 如何在Python中快速画图——使用Jupyter notebook的魔法函数(magic function)matplotlib inline

    如何在Python中快速画图--使用Jupyter notebook的魔法函数(magic function)matplotlib inline 先展示一段相关的代码: #we test the ac ...

  3. python中while循环和for循环的定义和详细的使用方法

    1. 循环的定义,反复做某事,具有明确的开始和结束.   2. 在Python中循环有while和for两种方式: While循环:1) 语法结构 >>> while 条件: ... ...

  4. python 中进制转换及format(),int()函数用法

    python中数值型变量好像只能是十进制形式表示,其他类型变量只能以字符串形式存在,可以通过format函数将int类型变量转换成其他进制字符串,如下所示: v_code=15 # 2进制 x=for ...

  5. Python中倒转输入序列元素顺序的reversed函数

    reversed函数将输入的序列的元素倒转后存储到一个类型为"reversed"可迭代对象,不能直接访问,可以转换为其他对象如列表或通过for循环方法访问. 注意:这里是倒转不是倒 ...

  6. python中的类的成员变量以及property函数

    1 python类的各种变量 1.1 全局变量 在类外定义的变量. 1.2 类变量 定义在类里面,所有的函数外面的变量.这个变量只有一份,是所有的对象共有的.在类外用“类.”来引用. 1.3 实例变量 ...

  7. 记录一些Python中不常用但非常好用的函数

    zfill(): 方法返回指定长度的字符串,原字符串右对齐,前面填充0. print('Helloworld'.zfill(50))0000000000000000000000000000000000 ...

  8. python中json文件处理涉及的四个函数json.dumps()和json.loads()、json.dump()和json.load()的区分

    一.概念理解 1.json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串) (1)json.dumps()函数是将一个Python数据类型列表进行js ...

  9. python中获取python版本号的方法【转】

    原文 python3 #!/usr/bin/python # 第1种方法 import platform print(platform.python_version()) >>> i ...

随机推荐

  1. builder模式实例

    package heapStark.blogCode.designPattern.builder; public class BaseBean { private int age; private S ...

  2. [Gamma]Scrum Meeting#8

    github 本次会议项目由PM召开,时间为6月3日晚上10点30分 时长15分钟 任务表格 人员 昨日工作 下一步工作 木鬼 撰写博客,组织例会 撰写博客,组织例会 swoip 前端显示屏幕,翻译坐 ...

  3. C# ini配置文件操作类

    /// <summary> /// INI文件操作类 /// </summary> public class IniFileHelper { /// <summary&g ...

  4. 【C/C++开发】C++11 并发指南三(std::mutex 详解)

    本系列文章主要介绍 C++11 并发编程,计划分为 9 章介绍 C++11 的并发和多线程编程,分别如下: C++11 并发指南一(C++11 多线程初探)(本章计划 1-2 篇,已完成 1 篇) C ...

  5. GCN(Graph Convolutional Network)的简单公式推导

    第一步:从前一个隐藏层到后一个隐藏层,对结点进行特征变换 第二步:对第一步进行具体实现 第三步:对邻接矩阵进行归一化(行之和为1) 邻接矩阵A的归一化,可以通过度矩阵D来实现(即通过D^-1*A来实现 ...

  6. Spring的JdbcTemplate使用教程

    Spring对数据库的操作在jdbc上面做了基本的封装,让开发者在操作数据库时只需关注SQL语句和查询 结果处理器,即可完成功能(当然,只使用JdbcTemplate,还不能摆脱持久层实现类的编写). ...

  7. | C语言I作业03

    | C语言I作业03 标签: 18软件 李煦亮 问题 答案 这个作业属于那个课程 C语言程序设计I 这个作业要求在哪里 https://edu.cnblogs.com 我在这个课程的目标是 学会和掌握 ...

  8. MySQL 索引小结

    1.!=.not in 在primary key上使用 !=.not in,explain 的 type 是 range,非primary key是全表扫描(即非主键字段即使有索引也无法应用) 2.a ...

  9. [转帖]美团在Redis上踩过的一些坑-5.redis cluster遇到的一些问题

    美团在Redis上踩过的一些坑-5.redis cluster遇到的一些问题 博客分类: redis 运维 redis clustercluster-node-timeoutfailover  转载请 ...

  10. AntDesign vue学习笔记(八)Table服务端分页使用

    本文是AntDesign后端分页方法 1.设置pagination <a-table :columns="columns" :dataSource="data&qu ...