Python记录器格式已损坏:I0716而不是INFO

Python记录器格式已损坏:I0716而不是INFO,python,tensorflow,logging,Python,Tensorflow,Logging,由于某种原因,python记录器格式有时会有点崩溃。我不确定出了什么问题,看起来像是编码问题: I0716 23:27:10.491452 4409853376 tpu_context.py:209]\u tpu context:eval_on_tpu True W0716 23:27:10.491577 4409853376 tpu_上下文。py:211]eval_on_tpu被忽略,因为use_tpu为False。 W0716 23:27:10.619174 4409853376弃用\u w

由于某种原因,python记录器格式有时会有点崩溃。我不确定出了什么问题,看起来像是编码问题:

I0716 23:27:10.491452 4409853376 tpu_context.py:209]\u tpu context:eval_on_tpu True
W0716 23:27:10.491577 4409853376 tpu_上下文。py:211]eval_on_tpu被忽略,因为use_tpu为False。
W0716 23:27:10.619174 4409853376弃用\u wrapper.py:119]来自bert-ner.py:423:名称tf.python\u io.TFRecordWriter已弃用。请改用tf.io.TFRecordWriter。
W0716 23:27:10.621710 4409853376弃用\u wrapper.py:119]来自bert-ner.py:428:名称tf.logging.info已弃用。请改用tf.compat.v1.logging.info。
应该是这样的:

信息。。。
信息。。。
警告
警告
脚本的执行方式如下:

subprocess.call('python3-bert-ner.py…',shell=True)
如何修复此问题?

Python API 如果您只想自定义
tensorflow
s日志格式,请在
absl
tensorflow
记录器中更换格式化程序:

import logging
from absl import logging as absl_logging
import tensorflow as tf


fmt = '[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s'
formatter = logging.Formatter(fmt)

absl_logging.get_absl_handler().setFormatter(formatter)
absl_logging.set_verbosity('debug')  # for example

for h in tf.get_logger().handlers:
    h.setFormatter(formatter)
tf.compat.v1.logging.set_verbosity(logging.DEBUG)  # for example


# test configuration
if __name__ == '__main__':
    # test custom logger if you have one - should also emit in the same format
    logging.getLogger(__name__).debug('tf imported')
    # this issues a DeprecationWarning in tensorflow>=1.13, emitting a warning log message
    s = tf.Session()
脚本将发出:

WARNING: Logging before flag parsing goes to stderr.
[DEBUG 2019-07-18 14:03:15,662 eggs.py:20] tf imported
[WARNING 2019-07-18 14:03:15,662 deprecation_wrapper.py:119] From eggs.py:22: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

2019-07-18 14:03:15.671392: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-07-18 14:03:15.674517: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 1992000000 Hz
...
C++ API Python部件发出的日志记录现在具有不同的统一输出。但是,正如您在上面的输出中看到的,从C++部分发出的日志不受此影响。不幸的是,C++代码中的日志格式目前无法配置(18-Jul-2019),请参阅IMPL:< /P>
void LogMessage::GenerateLogMessage(){
...
strftime(时间缓冲区,时间缓冲区大小,“%Y-%m-%d%H:%m:%S”,
本地时间(&now_秒));
//TODO(jeff,sanjay):将其替换为通过env记录的内容。
fprintf(标准,“%s.%06d:%c%s:%d]%s\n”、时间缓冲区、微秒余数、,
“IWEF”[severity],fname,line,str().c_str());
}

这意味着除了关闭库的C++部分的日志发射之外,你也做不了很多:

import logging
import os
from absl import logging as absl_logging
import tensorflow as tf

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

...
记录对齐 如果您和我一样,不喜欢日志行没有正确对齐,请对
fmt
string稍加修改:

fmt = '[%(levelname)8s %(asctime)s %(filename)s:%(lineno)s] %(message)s'
现在,输出看起来更具可读性:

[   DEBUG 2019-07-18 14:31:20,097 eggs.py:36] tf imported
[ WARNING 2019-07-18 14:31:20,097 deprecation_wrapper.py:119] From eggs.py:38: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.
使用微秒和线程ID 默认的
abseil
处理程序会将微秒添加到日期,还包括当前线程的ID。您也可以这样做-线程ID由
日志记录
模块提供,而必须首先计算微秒(我使用了一个自定义的
过滤器
)。上面的示例脚本,再次访问:

import logging
import os
import time
from absl import logging as absl_logging
import tensorflow as tf

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

fmt = '[%(levelname)8s %(asctime)s.%(microsecs)06d %(thread)d %(filename)s:%(lineno)s] %(message)s'
date_fmt = '%Y-%m-%d %H:%M:%S'
formatter = logging.Formatter(fmt, date_fmt)


class FilterMicroseconds(logging.Filter):
    def filter(self, record):
        local_time = time.localtime(record.created)
        record.microsecs = int(record.created % 1.0 * 1e6)
        return True

filter_microsecs = FilterMicroseconds()


absl_logging.get_absl_handler().setFormatter(formatter)
absl_logging.get_absl_handler().addFilter(filter_microsecs)
absl_logging.set_verbosity('debug')  # for example

for h in tf.get_logger().handlers:
    h.setFormatter(formatter)
    h.addFilter(filter_microsecs)
tf.compat.v1.logging.set_verbosity(logging.DEBUG)  # for example


# test configuration
if __name__ == '__main__':
    # test custom logger if you have one - should also emit in the same format
    logging.getLogger(__name__).debug('tf imported')
    # this issues a DeprecationWarning in tensorflow>=1.13, emitting a warning log message
    s = tf.Session()
输出:

WARNING: Logging before flag parsing goes to stderr.
[   DEBUG 2019-07-18 14:39:04.522035 140546459115328 eggs.py:36] tf imported
[ WARNING 2019-07-18 14:39:04.522186 140546459115328 deprecation_wrapper.py:119] From eggs.py:38: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

I0716
不是错误,而是7月16日
info消息的缩写,它来自
tensorflow
中的标准日志配置。我猜您的自定义日志配置(如果您有)没有被应用,那么可能会有所帮助。我根本没有为tensorflow设置自定义配置。将尝试添加,然后输出符合预期。对于自定义配置,调用带有所需设置的
logging.basicConfig()
就足够了。如果您想要模拟默认输出,我认为这个格式字符串应该是一个好的开始:
[%(levelname)%(asctime)s%(filename)s:%(lineno)s]%(message)s
,但是,如果您想要完全相同的格式,您需要一个自定义的
date\u fmt
,并实现一个格式化程序,该格式化程序将以微秒为单位计算时间戳,因为用户不提供时间戳。@hoefling,谢谢您的解释。现在我至少明白发生了什么。很高兴我能帮上忙!我还注意到我在上面的评论中发布的格式字符串中有一个错误;它应该是
[%(levelname)s%(asctime)s%(filename)s:%(lineno)s](message)s
谢谢!真棒的导游。我唯一找不到的消息是:
W0725 16:57:55.042182 4446524864弃用\u wrapper.py:119]From…/bert/optimization.py:84:名称tf.train.Optimizer已弃用。请改用tf.compat.v1.train.Optimizer。
答案中的代码应涵盖弃用包装;包括
tf.Session
警告的示例输出。使用
tf.train.Optimizer
测试上述代码,修改输出。再次检查您的代码-如果没有,可能会问另一个问题,并在这里的评论中删除一个链接ping me。