TypechoJoeTheme

Misty rain的博客

统计

python公共类集合(获取时间,打印日志,获取文件目录)

2021-12-20
/
78 评论
/
709 阅读
/
正在检测是否收录...
12/20

获取时间公共类

"""
@Author:Misty rain(ZhangHao)
@E-mail:676817831@qq.com
@FileName:gettime.py
@Software:PyCharm
@Desc:获取时间参数
"""
import time
import datetime


def get_system_time():
    """
        获取当前时间
        :return:
        """
    new_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    return str(new_time)


def get_system_time_nyr():
    """
        获取当前日期
        :return:
        """
    new_time = time.strftime('%Y-%m-%d', time.localtime())
    return str(new_time)


def get_system_time_nyrsf():
    """
        获取当前时间 具体到时分
        :return:
        """
    new_time = time.strftime('%Y-%m-%d %H:%M', time.localtime())

    return str(new_time)


def get_system_time_hdata():
    """
        获取当前时间的后一天
        :return:
        """
    now_time = datetime.datetime.now()
    new_time = (now_time + datetime.timedelta(days=+1)).strftime("%Y-%m-%d %M:%S")

    return str(new_time)


def get_system_time_tdata():
    """
        获取当前时间的后二天
        :return:
        """
    now_time = datetime.datetime.now()
    new_time = (now_time + datetime.timedelta(days=+2)).strftime("%Y-%m-%d %M:%S")

    return str(new_time)


def get_logdata():
    """
        获取当前日期
        :return:
        """
    new_time = time.strftime('%Y%m%d%H%M%S', time.localtime())

    return str(new_time)


def get_system_time_hhours():
    """
        获取当前时间的后一小时
        :return:
        """
    now_time = datetime.datetime.now()
    new_time = (now_time + datetime.timedelta(hours=+1)).strftime("%Y-%m-%d %H:%M")

    return str(new_time)


def get_system_time_hdkssj():
    """
        获取当前时间的后俩小时
        :return:
        """
    now_time = datetime.datetime.now()
    new_time = (now_time + datetime.timedelta(hours=+2)).strftime("%Y-%m-%d %H:%M")

    return str(new_time)

获取文件目录,获取当前目录

"""
@Author:Misty rain(ZhangHao)
@E-mail:676817831@qq.com
@FileName:getfileposition.py
@Software:PyCharm
@Desc:获取文件位置公共类
"""
import os


# 获取当前目录
def getcurrent():
    path = os.path.abspath(os.path.dirname(__file__))
    return path


# 获取项目根目录
def getrootpath():
    path = os.path.dirname(os.path.dirname(__file__))
    return path


# 获取文件目录
def getfilepath():
    path = os.path.dirname(os.path.dirname(__file__)) + '/file'
    return path


# 调试
if __name__ == '__main__':
    print(getcurrent())
    print(getrootpath())
    print(getfilepath())

日志公共类

"""
@Author:Misty rain(ZhangHao)
@E-mail:676817831@qq.com
@FileName:getlog.py
@Software:PyCharm
@Desc:
"""
import logging
import commont.getfileposition as file
import colorlog
import commont.gettime as time


# 定义全局变量
log_colors_config = {
    'DEBUG': 'white',
    'INFO': 'green',
    'WARNING': 'yellow',
    'ERROR': 'red',
    'CRITICAL': 'bold_red'
}

logger = logging.getLogger('PROJECT')
logger.setLevel(logging.DEBUG)

# 创建一个handler写入日志
fh = logging.FileHandler(
    filename=file.getrootpath()+'/log/{project}.log'.format(project=time.get_system_time_nyr()))
fh.setLevel(logging.DEBUG)

# 创建一个handler输出控制台
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# 定义日志输入格式
formatter = logging.Formatter(
    fmt='[%(asctime)s.%(msecs)03d] %(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s',
    datefmt='%Y-%m-%d  %H:%M:%S'
)
# 控制台输出有颜色的日志
console_formatter = colorlog.ColoredFormatter(
    fmt='%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] '
        ': %(message)s',
    datefmt='%Y-%m-%d  %H:%M:%S',
    log_colors=log_colors_config
)

# 定义日志输入格式
# formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s','%Y/%m/%d %H:%M:%S')
fh.setFormatter(formatter)
ch.setFormatter(console_formatter)

logger.addHandler(fh)
logger.addHandler(ch)

if __name__ == '__main__':

    logger.error('error message')
    logger.debug('debug message')
    logger.info('info message')
    logger.warning('war message')
    logger.critical('cri message')
python
朗读
赞(1)
版权属于:

Misty rain的博客

本文链接:

http://101.42.223.25/index.php/archives/30/(转载时请注明本文出处及文章链接)

评论 (78)
  1. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  2. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  3. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  4. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  5. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  6. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  7. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  8. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  9. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  10. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  11. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  12. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  13. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  14. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  15. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  16. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  17. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  18. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  19. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  20. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  21. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  22. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  23. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  24. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  25. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  26. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  27. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  28. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  29. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  30. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  31. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  32. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  33. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  34. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  35. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  36. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  37. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  38. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  39. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  40. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  41. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  42. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  43. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  44. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  45. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  46. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  47. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  48. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  49. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  50. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  51. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  52. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  53. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  54. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  55. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  56. 1 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  57. 1 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  58. -1 OR 2+269-269-1=0+0+0+1 -- 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  59. the 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  60. the 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  61. the 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  62. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  63. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  64. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  65. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  66. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  67. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  68. 1 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  69. 1 作者
    Windows 10 · Google Chrome

    555

    2022-04-28 回复
  70. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  71. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  72. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  73. the 作者
    Windows 10 · Google Chrome

    response.write(9679664*9332124)

    2022-04-28 回复
  74. the 作者
    Windows 10 · Google Chrome

    '+response.write(9679664*9332124)+'

    2022-04-28 回复
  75. the 作者
    Windows 10 · Google Chrome

    "+response.write(9679664*9332124)+"

    2022-04-28 回复
  76. the 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复
  77. the 作者
    Windows 10 · Google Chrome

    -5 OR 535=(SELECT 535 FROM PG_SLEEP(15))--

    2022-04-28 回复
  78. 1 作者
    Windows 10 · Google Chrome

    1

    2022-04-28 回复

备案号: 浙ICP备2021040483号