如何在谷歌云功能中打印日语日志



我使用logback在java中的谷歌云函数中打印日志。我想打印日本日志,但日志上都是奇怪的字符。如何在Java代码中打印日志

我的logback设置低于

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="jsonConsoleAppender"
class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>[ignore]</timestamp>
<version>[ignore]</version>
<logger>[ignore]</logger>
<thread>[ignore]</thread>
<level>[ignore]</level>
<levelValue>[ignore]</levelValue>
</fieldNames>
<charset class="java.nio.charset.Charset">UTF-8</charset>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="jsonConsoleAppender"/>
</root>
</configuration>

我的代码打印日志

logger.info("処理開始:加工前処理", kv("severity", "NOTICE"));

日志是

{
"textPayload": "������������:���������������",
"insertId": "000000-47138e32-2a00-40ec-ad45-88c03dffc271",
"resource": {
"type": "cloud_function",
"labels": {
"project_id": "xxxxxx",
"region": "asia-northeast1",
"function_name": "function-2"
}
},
"timestamp": "2020-11-05T09:24:46.633Z",
"severity": "NOTICE",
"labels": {
"execution_id": "sa3kufvievy8"
},
"logName": "projects/xxxxxx/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
"trace": "projects/xxxxxx/traces/17915c7ed95b6d275b424e95f1a5b94b",
"receiveTimestamp": "2020-11-05T09:24:56.671368622Z"
}

这里提到的GCP函数似乎不太支持Logback。所以我建议使用Java Logging API,它已经包含在云函数中。

一个示例代码是:

文件夹结构

src->main-->java->com.>示例-->示例.java

pom.xml

示例.java

package com.example;
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.logging.Logger;
public class Example implements HttpFunction {
private static final Logger logger = Logger.getLogger(Example.class.getName());
@Override
public void service(HttpRequest request, HttpResponse response)
throws IOException {
logger.info("処理開始:加工前処理");
BufferedWriter writer = response.getWriter();
writer.write("Messages successfully logged!");
}
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.cloud.functions</groupId>
<artifactId>functions-logging-log-hello-world</artifactId>
<parent>
<groupId>com.google.cloud.samples</groupId>
<artifactId>shared-configuration</artifactId>
<version>1.0.21</version>
</parent>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- Required for Function primitives -->
<dependency>
<groupId>com.google.cloud.functions</groupId>
<artifactId>functions-framework-api</artifactId>
<version>1.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!--
Google Cloud Functions Framework Maven plugin
This plugin allows you to run Cloud Functions Java code
locally. Use the following terminal command to run a
given function locally:
mvn function:run -Drun.functionTarget=your.package.yourFunction
-->
<groupId>com.google.cloud.functions</groupId>
<artifactId>function-maven-plugin</artifactId>
<version>0.9.5</version>
<configuration>
<functionTarget>functions.Example</functionTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- version 3.0.0-M4 does not load JUnit5 correctly -->
<!-- see https://issues.apache.org/jira/browse/SUREFIRE-1750 -->
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<skipTests>${skipTests}</skipTests>
<reportNameSuffix>sponge_log</reportNameSuffix>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
</plugins>
</build>
</project>

相关内容

  • 没有找到相关文章

最新更新