示意代码如下:

#_*_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. 解决coursera笔记本(ipynb)打不开的问题

    最近在coursera听课,发现notebook打不开,一直显示加载中. 想到了可能被墙,苦于没有梯子.最后F12,发现有报错 Failed to load resource: net::ERR_CO ...

  2. Python爬虫爬取BT之家找电影资源

    一.写在前面 最近看新闻说圣城家园(SCG)倒了,之前BT天堂倒了,暴风影音也不行了,可以说看个电影越来越费力,国内大厂如企鹅和爱奇艺最近也出现一些幺蛾子,虽然目前版权意识虽然越来越强,但是很多资源在 ...

  3. Java 通过SFTP上传图片功能

    1.需要在pom.xml文件中引用jsch的依赖: <dependency> <groupId>com.jcraft</groupId> <artifactI ...

  4. ORM基础知识

    ORM基础知识 一.什么ORM? ORM是Object Relactional Mapping的缩写,即对象关系映射,是将关系型数据库中的数据库结构映射成对象,就可以通过面向对象思想编程. 二.常用的 ...

  5. SWIG 3 中文手册——2. 引言

    目录 2 引言 2.1 SWIG 是什么? 2.2 为什么使用 SWIG? 2.3 一个 SWIG 示例 2.3.1 SWIG 接口文件 2.3.2 swig 命令 2.3.3 构建 Perl5 模块 ...

  6. C++ getline()的两种用法

    getline():用于读入一整行的数据.在C++中,有两种getline函数.第一种定义在头文件<istream>中,是istream类的成员函数:第二种定义在头文件<string ...

  7. Sitecore安全性第1部分:自定义角色和权限

    安全性是任何Sitecore构建的重要组成部分.它可确保您的内容作者具有适当级别的访问权限,以管理他们拥有的内容,并授予他们访问不同Sitecore功能的权限. Sitecore附带了许多提供功能访问 ...

  8. word 转 pfd

    转自: https://www.cnblogs.com/qiwu1314/p/6101400.html demo: public class Doc2Pdf { public static boole ...

  9. java实现二维码的生成和解读

    Java利用QRCode.jar包实现二维码编码与解码   QRcode是日本人94年开发出来的.首先去QRCode的官网http://swetake.com/qrcode/java/qr_java. ...

  10. c/c++封装成python包

    参考网址:https://blog.csdn.net/tiankongtiankong01/article/details/80420033 SWIG (Simplified Wrapper and ...