我正在traits2.rsruslings 中进行练习,并且对 Rust 的特征语法感到困惑。我有以下工作解决方案(编译并通过测试,我使用 Rust 1.50):
trait AppendBar {
fn append_bar(self) -> Self;
}
impl AppendBar for Vec<String> {
fn append_bar(mut self) -> Self {
self.push("Bar".into());
self
}
}
Run Code Online (Sandbox Code Playgroud)
然而,我感到困惑的是,虽然特征定义是fn append_bar(self) -> Self,但我的实现是,它在签名上fn append_bar(mut self) -> Self有一个附加的。mut为什么这是允许的?
我正在使用 python 标准库中的日志记录模块,并希望获取当前的Formatter. 原因是我正在使用multiprocessing模块,并且对于每个进程,我想为它的记录器分配另一个文件处理程序以记录到它自己的日志文件中。当我按以下方式执行此操作时
logger = logging.getLogger('subprocess')
log_path = 'log.txt'
with open(log_path, 'a') as outfile:
handler = logging.StreamHandler(outfile)
logger.addHandler(handler)
Run Code Online (Sandbox Code Playgroud)
中的消息log.txt根本没有格式,但我希望该消息的格式与我的典型日志记录格式相同。我的典型日志记录设置如下所示
logging.basicConfig(
format='%(asctime)s | %(levelname)s | %(name)s - %(process)d | %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=os.environ.get('LOGLEVEL', 'INFO').upper(),
)
logger = logging.getLogger(__name__)
Run Code Online (Sandbox Code Playgroud)
由于看起来该Formatter对象与该对象相关联Handler,因此我尝试从Logger具有正确格式的主对象获取处理程序。所以我打电话
logger.handlers
Run Code Online (Sandbox Code Playgroud)
但我得到了一个空清单[]。
所以我的问题是,我在哪里可以获得Formatter与我的主记录器具有相同格式的对象?
根据记录,我在 macOS 上使用 python 3.8,但代码将部署到 Linux(但仍然是 python 3.8)。