如何在 Google Cloud Functions 中打印日语日志
How to print japanese log in Google Cloud Functions
我在 java 中使用 logback 打印登录 Google Cloud Functions。
我想打印日文日志,但是日志全是奇怪的字符。
如何在我的 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"
}
看起来 Logback 在 GCP 功能中没有得到很好的支持,如 mentioned here。所以我建议使用已经包含在 Cloud Functions 中的 Java Logging API。
示例代码为:
文件夹结构:
src --> main --> java --> com --> example --> Example.java
pom.xml
Example.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>
我在 java 中使用 logback 打印登录 Google Cloud Functions。 我想打印日文日志,但是日志全是奇怪的字符。 如何在我的 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"
}
看起来 Logback 在 GCP 功能中没有得到很好的支持,如 mentioned here。所以我建议使用已经包含在 Cloud Functions 中的 Java Logging API。
示例代码为:
文件夹结构:
src --> main --> java --> com --> example --> Example.java
pom.xml
Example.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>