首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Google函数中打印日文日志

如何在Google函数中打印日文日志
EN

Stack Overflow用户
提问于 2020-11-05 10:48:13
回答 1查看 131关注 0票数 0

我使用logback在java中的Google函数中打印日志。我想打印日文日志,但这些日志都是奇怪的字符。如何在Java代码中打印日志?

我的logback设置在下面

代码语言:javascript
复制
<?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>

我打印日志的代码

代码语言:javascript
复制
logger.info("処理開始:加工前処理", kv("severity", "NOTICE"));

原木是

代码语言:javascript
复制
{
    "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"
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-05 12:00:41

看起来,GCP函数并不像这里提到的那样很好地支持Logback。因此,我建议使用已经包含在云函数中的Java日志API。

一个示例代码是:

文件夹结构

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

pom.xml

Example.java

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64695620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档