|
| 1 | +/* |
| 2 | + * Copyright [2019] [恒宇少年 - 于起宇] |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package org.minbox.framework.api.boot.plugin.logging.admin.listener; |
| 19 | + |
| 20 | +import com.alibaba.fastjson.JSON; |
| 21 | +import org.minbox.framework.api.boot.plugin.logging.ApiBootLogClientNotice; |
| 22 | +import org.minbox.framework.api.boot.plugin.logging.admin.endpoint.LoggingEndpoint; |
| 23 | +import org.minbox.framework.api.boot.plugin.logging.admin.event.ReportLogEvent; |
| 24 | +import org.minbox.framework.api.boot.plugin.tools.JsonTools; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | +import org.springframework.context.ApplicationEvent; |
| 28 | +import org.springframework.context.event.SmartApplicationListener; |
| 29 | + |
| 30 | +/** |
| 31 | + * Report Log Json Format Listener |
| 32 | + * |
| 33 | + * @author:恒宇少年 - 于起宇 |
| 34 | + * <p> |
| 35 | + * DateTime:2019-07-22 15:24 |
| 36 | + * Blog:http://blog.yuqiyu.com |
| 37 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 38 | + * Gitee:https://gitee.com/hengboy |
| 39 | + * GitHub:https://github.com/hengboy |
| 40 | + * @see ReportLogEvent |
| 41 | + */ |
| 42 | +public class ReportLogJsonFormatListener implements SmartApplicationListener { |
| 43 | + /** |
| 44 | + * logger instance |
| 45 | + */ |
| 46 | + static Logger logger = LoggerFactory.getLogger(ReportLogJsonFormatListener.class); |
| 47 | + /** |
| 48 | + * Whether to print the logs reported on the console |
| 49 | + */ |
| 50 | + private boolean showConsoleReportLog; |
| 51 | + /** |
| 52 | + * Format console log JSON |
| 53 | + */ |
| 54 | + private boolean formatConsoleLogJson; |
| 55 | + |
| 56 | + public ReportLogJsonFormatListener(boolean showConsoleReportLog, boolean formatConsoleLogJson) { |
| 57 | + this.showConsoleReportLog = showConsoleReportLog; |
| 58 | + this.formatConsoleLogJson = formatConsoleLogJson; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Report logs on console output |
| 63 | + * Format Update Log |
| 64 | + * |
| 65 | + * @param event ReportLogEvent |
| 66 | + */ |
| 67 | + @Override |
| 68 | + public void onApplicationEvent(ApplicationEvent event) { |
| 69 | + try { |
| 70 | + ReportLogEvent reportLogEvent = (ReportLogEvent) event; |
| 71 | + if (showConsoleReportLog) { |
| 72 | + ApiBootLogClientNotice notice = reportLogEvent.getLogClientNotice(); |
| 73 | + String serviceInfo = String.format("%s -> %s", notice.getClientServiceId(), notice.getClientServiceIp()); |
| 74 | + logger.info("Receiving Service: 【{}】, Request Log Report,Logging Content:{}", serviceInfo, formatConsoleLogJson ? JsonTools.beautifyJson(notice.getLoggers()) : JSON.toJSONString(notice.getLoggers())); |
| 75 | + } |
| 76 | + } catch (Exception e) { |
| 77 | + logger.error(e.getMessage(), e); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean supportsSourceType(Class<?> sourceType) { |
| 83 | + return sourceType == LoggingEndpoint.class; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) { |
| 88 | + return eventType == ReportLogEvent.class; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * first execute |
| 93 | + * |
| 94 | + * @return execute order |
| 95 | + */ |
| 96 | + @Override |
| 97 | + public int getOrder() { |
| 98 | + return 0; |
| 99 | + } |
| 100 | +} |
0 commit comments