如何在 monolog 中将日志设置为 json格式 ....
message{a="something";b="something else"}
这似乎不正确,因为它显示错误
调用未定义的方法Monolog \ Logger :: setFormatter()
我也希望我的日志记录如下:
<plugin> <!--Compiler instructions to generate model, add to sources.-->
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>
</compilerArguments>
</configuration>
</plugin>
<plugin><!--path were to generate model, add to -->
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<executions>
<execution>
<id>process</id>
<goals>
<goal>process</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<!-- source output directory -->
<outputDirectory>target/metamodel</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/metamodel</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
而不是:
public class MyControl : ContentView // WPF: inherited from UserControl
{
// Xamarin:
public static readonly BindableProperty MyValueProperty = ...
// WPF:
// public static readonly DependencyProperty MyValueProperty = ...
public int MyValue
{
get { return (int) GetValue(MyValueProperty); }
set { SetValue(MyValueProperty, value); }
}
public void Reset()
{
MyValue = 0;
}
}
答案 0 :(得分:0)
如果其他人也有这个问题:格式化程序可以设置为处理程序,而不是记录器。
use Monolog\Formatter\JsonFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
$logger = new Logger('your_channel_name');
$handler = new StreamHandler('your/dir.log');
$handler->setFormatter( new JsonFormatter() );
$logger->pushHandler($handler);
// and then where ever in the code
$logger->info('your message', ["var1" => "value 1", "otherVar" => "otherValue"]);
// it will result in a parsable output similar to this:
// {
// "message":"your message",
// "context": {
// "var1":"value 1",
// "otherVar":"otherValue"
// },
// "level":200,
// "level_name":"INFO",
// "channel":"your_channel_name",
// "datetime":"2021-06-25T10:54:10.116817+00:00",
// "extra":{}
// }
documentation 本身并不是最好的,但也是一个开始。