栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在GCP Felexible非兼容App Engine中将Java应用日志事件映射到相应的云日志事件级别?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在GCP Felexible非兼容App Engine中将Java应用日志事件映射到相应的云日志事件级别?

这是我如何使用SLF4J使云日志记录工作。这适用于不兼容的Java GAE Flex环境。

logback.xml

<configuration debug="true">    <appender name="FILE" >        <file>/var/log/app_engine/custom_logs/app.log.json</file>        <append>true</append>        <!-- enprers are assigned the type ch.qos.logback.classic.enprer.PatternLayoutEnprer by default -->        <enprer > <layout     >     <pattern>%-4relative [%thread] %-5level %logger{35} - %msg</pattern> </layout>        </enprer>    </appender>    <root level="DEBUG">        <appender-ref ref="FILE" />    </root></configuration>

这是我用来在日志文件中的一行上生成JSON的PatternLayout类。

import static ch.qos.logback.classic.Level.DEBUG_INT;import static ch.qos.logback.classic.Level.ERROR_INT;import static ch.qos.logback.classic.Level.INFO_INT;import static ch.qos.logback.classic.Level.TRACE_INT;import static ch.qos.logback.classic.Level.WARN_INT;import java.util.Map;import org.json.JSONObject;import com.homedepot.ta.wh.common.logging.GCPCloudLoggingJSONLayout.GCPCloudLoggingEvent.GCPCloudLoggingTimestamp;import ch.qos.logback.classic.Level;import ch.qos.logback.classic.PatternLayout;import ch.qos.logback.classic.spi.ILoggingEvent;public class GCPCloudLoggingJSonLayout extends PatternLayout {    @Override    public String doLayout(ILoggingEvent event) {        String formattedMessage = super.doLayout(event);        return doLayout_internal(formattedMessage, event);    }        String doLayout_internal(String formattedMessage, ILoggingEvent event) {        GCPCloudLoggingEvent gcpLogEvent = new GCPCloudLoggingEvent(formattedMessage  , convertTimestampToGCPLogTimestamp(event.getTimeStamp())  , mapLevelToGCPLevel(event.getLevel())  , null);        JSonObject jsonObj = new JSonObject(gcpLogEvent);                return jsonObj.toString() + "n";     }    static GCPCloudLoggingTimestamp convertTimestampToGCPLogTimestamp(long millisSinceEpoch) {        int nanos = ((int) (millisSinceEpoch % 1000)) * 1_000_000; // strip out just the milliseconds and convert to nanoseconds        long seconds = millisSinceEpoch / 1000L; // remove the milliseconds        return new GCPCloudLoggingTimestamp(seconds, nanos);    }    static String mapLevelToGCPLevel(Level level) {        switch (level.toInt()) {        case TRACE_INT: return "TRACE";        case DEBUG_INT: return "DEBUG";        case INFO_INT: return "INFO";        case WARN_INT: return "WARN";        case ERROR_INT: return "ERROR";        default: return null;         }    }        public static class GCPCloudLoggingEvent {        private String message;        private GCPCloudLoggingTimestamp timestamp;        private String traceId;        private String severity;        public GCPCloudLoggingEvent(String message, GCPCloudLoggingTimestamp timestamp, String severity,     String traceId) { super(); this.message = message; this.timestamp = timestamp; this.traceId = traceId; this.severity = severity;        }        public String getMessage() { return message;        }        public void setMessage(String message) { this.message = message;        }        public GCPCloudLoggingTimestamp getTimestamp() { return timestamp;        }        public void setTimestamp(GCPCloudLoggingTimestamp timestamp) { this.timestamp = timestamp;        }        public String getTraceId() { return traceId;        }        public void setTraceId(String traceId) { this.traceId = traceId;        }        public String getSeverity() { return severity;        }        public void setSeverity(String severity) { this.severity = severity;        }                public static class GCPCloudLoggingTimestamp { private long seconds; private int nanos; public GCPCloudLoggingTimestamp(long seconds, int nanos) {     super();     this.seconds = seconds;     this.nanos = nanos; } public long getSeconds() {     return seconds; } public void setSeconds(long seconds) {     this.seconds = seconds; } public int getNanos() {     return nanos; } public void setNanos(int nanos) {     this.nanos = nanos; }        }}    @Override    public Map<String, String> getDefaultConverterMap() {        return PatternLayout.defaultConverterMap;    }   }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430897.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号