简体   繁体   English

Elixir:如何将记录器连接到特定的记录器后端

[英]Elixir: How can I hookup a logger to a specific logger backend

I have a situation like this:我有这样的情况:

In a Phoenix application, I have some protected information, and want to log those information in different ways to different places.在 Phoenix 应用程序中,我有一些受保护的信息,并希望以不同的方式将这些信息记录到不同的地方。

For example:例如:

EncryptedLogger => Log to LoggerBackend 1 => write to machine 1 EncryptedLogger => 记录到 LoggerBackend 1 => 写入机器 1

PlainLogger => Log to LoggerBacken 2 => write to machine 2 PlainLogger => 记录到 LoggerBacken 2 => 写入机器 2

LoggerBackend 1 and LoggerBackend 2 can be the same type of custom logger backend, but configured differently to point to machine 1 and machine 2 respectively. LoggerBackend 1 和 LoggerBackend 2 可以是相同类型的自定义记录器后端,但配置不同以分别指向机器 1 和机器 2。 This part I have already done.这部分我已经做了。

The thing I don't know how to do is: How to hook EncryptedLogger only to Backend 1 and PlainLogger only to Backend 2.我不知道该怎么做的是:如何仅将 EncryptedLogger 挂钩到后端 1,而将 PlainLogger 仅挂钩到后端 2。

We know that for Elixir, a call to Logger will write info to all the backends that are specified in app config.我们知道,对于 Elixir,调用 Logger 会将信息写入应用配置中指定的所有后端。 Currently, any call on any loggers will write to both Backend 1 and Backend 2.目前,对任何记录器的任何调用都将写入后端 1 和后端 2。

Question: Is it possible to easily hook specific loggers to only specific backends?问题:是否可以轻松地将特定记录器仅挂接到特定后端? (Not all the backends) (并非所有后端)

Here are the requirements:以下是要求:

1- Retain as much as possible the Logger calls in the code. 1- 尽可能多地保留代码中的 Logger 调用。 We have a lot of Logger calls in the code, so ideally we should have to change only minimum log calls that log the PHI information.我们在代码中有很多 Logger 调用,所以理想情况下,我们应该只更改记录 PHI 信息的最少日志调用。 The other calls must just work.其他调用必须正常工作。

2- For each of the call to the Logger, for example Logger.debug(“Call list_user:” … whatever users list from database here) , it must do the following things: - Log to the extremely secured PHI log server, send only information that is PHI, so we don't redundantly write Non-PHI information there. 2- 对于每个对 Logger 的调用,例如Logger.debug(“Call list_user:” … whatever users list from database here) ,它必须执行以下操作: - 登录到极其安全的 PHI 日志服务器,仅发送PHI 信息,所以我们不会在那里多余地写非 PHI 信息。 - Log to the Non-PHI log server: All non-phi information is stored as they are, and all the phi information is stored with phi data masked as “*” - 登录到非PHI日志服务器:所有非PHI信息都按原样存储,所有phi信息存储时将phi数据屏蔽为“*”

3- All the current logger backends, for example: :console, LoggerFileBackend should still work with no modification, and they must log non-phi data, phi-data securely, meaning no real PHI data can be printed to those existing logger backends. 3- 所有当前的记录器后端,例如::console、LoggerFileBackend 应该仍然可以在不修改的情况下工作,并且它们必须安全地记录非 phi 数据、phi-data,这意味着不能将真正的 PHI 数据打印到那些现有的记录器后端。

4- When people add more Logger backends to log to different services, for example Timber or Spark, it must be incorporated in to PHI – Non PHI tasks seamlessly. 4- 当人们添加更多 Logger 后端来登录不同的服务时,例如 Timber 或 Spark,它必须无缝地合并到 PHI – 非 PHI 任务中。 If they misconfigure the logger, the PHI information should not go there unmasked.如果他们错误地配置了记录器,PHI 信息不应该被暴露。 And the existing calls to Logger in the current application code must not be changed.并且不得更改当前应用程序代码中对 Logger 的现有调用。

5- When software engineers call the Logger.info, Logger.debug, Logger.error in the code, the PHI information cannot be accidentally leaked. 5- 当软件工程师调用代码中的Logger.info、Logger.debug、Logger.error时,PHI信息不会被意外泄露。 For example some existing code or new code like this: Logger.error(“Error updating user: #{inspect(user)}) will automatically print the masked version of data to log, instead of the original.例如,一些现有代码或新代码如下: Logger.error(“Error updating user: #{inspect(user)})将自动打印要记录的数据的屏蔽版本,而不是原始数据。

It is not possible currently.目前是不可能的。 My suggestion would be to bypass the Logger infrastructure and directly invoke and write to the EncryptedFile/EncryptedBackend.我的建议是绕过 Logger 基础设施,直接调用和写入 EncryptedFile/EncryptedBackend。

Here is my solution for this problem: http://hanoian.com/content/index.php/28-elixir-logger-to-different-backends-selectively-with-different-info这是我对这个问题的解决方案: http : //hanoian.com/content/index.php/28-elixir-logger-to-different-backends-selectively-with-different-info

The whole explanation is rather long, with a github link to the demo.整个解释相当长,附有演示的 github 链接。 But the gist of it is that created a proxy backend to receive all the Logger's calls, then the proxy backend will call different backends in different ways to send different types of information.但它的要点是创建了一个代理后端来接收所有 Logger 的调用,然后代理后端会以不同的方式调用不同的后端来发送不同类型的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM