From df2048eeedd53536eae88c8a69a6da18ad847eaa Mon Sep 17 00:00:00 2001 From: feilong Date: Fri, 7 Jun 2024 11:51:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BAcom.feilong.net.bot.LogAndBot?= =?UTF-8?q?Combination.error(CombinationConfig,=20String,=20Object...)=20f?= =?UTF-8?q?ix=20#712?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新建com.feilong.net.bot.LogAndBotCombination.warn(CombinationConfig, String, Object...) fix #711 新建com.feilong.net.bot.LogAndBotCombination.info(CombinationConfig, String, Object...) fix #710 新建 com.feilong.net.bot.LogAndBotCombination.debug(CombinationConfig, String, Object...) 机器人支持是否发机器人的开关 fix #709 --- .../feilong/net/bot/CombinationConfig.java | 226 ++++++++++++++++++ .../feilong/net/bot/LogAndBotCombination.java | 105 +++++++- 2 files changed, 328 insertions(+), 3 deletions(-) create mode 100644 feilong-net/feilong-net-bot/feilong-net-bot-api/src/main/java/com/feilong/net/bot/CombinationConfig.java diff --git a/feilong-net/feilong-net-bot/feilong-net-bot-api/src/main/java/com/feilong/net/bot/CombinationConfig.java b/feilong-net/feilong-net-bot/feilong-net-bot-api/src/main/java/com/feilong/net/bot/CombinationConfig.java new file mode 100644 index 00000000..d58b3d4f --- /dev/null +++ b/feilong-net/feilong-net-bot/feilong-net-bot-api/src/main/java/com/feilong/net/bot/CombinationConfig.java @@ -0,0 +1,226 @@ +/* + * Copyright (C) 2008 feilong + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.feilong.net.bot; + +import org.slf4j.Logger; + +/** + * 混合相关配置. + * + * @author feilong + * @since 4.1.0 + */ +public class CombinationConfig{ + + /** 使用的logger. */ + private Logger logger; + + /** 使用的bot. */ + private Bot bot; + + //--------------------------------------------------------------- + /** 签名. */ + private String signName; + + /** 是否输出日志, 默认是true , 如果是false 那么不输出日志. */ + private boolean isPrintLog = true; + + /** + * 是否发送机器人消息, 默认是true , 如果是false 那么不发送消息. + * + *

+ * 场景: 原先有相关代码发送了消息, 后来需要加个逻辑, 判定部分id不发送 可以把这个值设置为false + *

+ */ + private boolean isSendBotMessage = true; + + //--------------------------------------------------------------- + + /** + * Instantiates a new combination config. + */ + public CombinationConfig(){ + super(); + } + + /** + * Instantiates a new combination config. + * + * @param logger + * the logger + * @param bot + * the bot + * @param isSendBotMessage + * the is send bot message + */ + public CombinationConfig(Logger logger, Bot bot, boolean isSendBotMessage){ + super(); + this.logger = logger; + this.bot = bot; + this.isSendBotMessage = isSendBotMessage; + } + + /** + * Instantiates a new combination config. + * + * @param logger + * the logger + * @param bot + * the bot + * @param signName + * the sign name + * @param isSendBotMessage + * 是否发送机器人消息, 默认是true , 如果是false 那么不发送消息. + * + *

+ * 场景: 原先有相关代码发送了消息, 后来需要加个逻辑, 判定部分id不发送 可以把这个值设置为false + *

+ */ + public CombinationConfig(Logger logger, Bot bot, String signName, boolean isSendBotMessage){ + super(); + this.logger = logger; + this.bot = bot; + this.signName = signName; + this.isSendBotMessage = isSendBotMessage; + } + + //--------------------------------------------------------------- + + /** + * Instantiates a new combination config. + * + * @param logger + * the logger + * @param bot + * the bot + * @param signName + * the sign name + * @param isPrintLog + * the is print log + * @param isSendBotMessage + * 是否发送机器人消息, 默认是true , 如果是false 那么不发送消息. + * + *

+ * 场景: 原先有相关代码发送了消息, 后来需要加个逻辑, 判定部分id不发送 可以把这个值设置为false + *

+ */ + public CombinationConfig(Logger logger, Bot bot, String signName, boolean isPrintLog, boolean isSendBotMessage){ + super(); + this.logger = logger; + this.bot = bot; + this.signName = signName; + this.isPrintLog = isPrintLog; + this.isSendBotMessage = isSendBotMessage; + } + + //--------------------------------------------------------------- + + /** + * 获得 使用的logger. + * + * @return the logger + */ + public Logger getLogger(){ + return logger; + } + + /** + * 设置 使用的logger. + * + * @param logger + * the logger to set + */ + public void setLogger(Logger logger){ + this.logger = logger; + } + + /** + * 获得 使用的bot. + * + * @return the bot + */ + public Bot getBot(){ + return bot; + } + + /** + * 设置 使用的bot. + * + * @param bot + * the bot to set + */ + public void setBot(Bot bot){ + this.bot = bot; + } + + /** + * 获得 签名. + * + * @return the signName + */ + public String getSignName(){ + return signName; + } + + /** + * 设置 签名. + * + * @param signName + * the signName to set + */ + public void setSignName(String signName){ + this.signName = signName; + } + + /** + * 获得 是否输出日志, 默认是true , 如果是false 那么不输出日志. + * + * @return the isPrintLog + */ + public boolean getIsPrintLog(){ + return isPrintLog; + } + + /** + * 获得 是否发送机器人消息, 默认是true , 如果是false 那么不发送消息. + * + * @return the isSendBotMessage + */ + public boolean getIsSendBotMessage(){ + return isSendBotMessage; + } + + /** + * 设置 是否输出日志, 默认是true , 如果是false 那么不输出日志. + * + * @param isPrintLog + * the isPrintLog to set + */ + public void setIsPrintLog(boolean isPrintLog){ + this.isPrintLog = isPrintLog; + } + + /** + * 设置 是否发送机器人消息, 默认是true , 如果是false 那么不发送消息. + * + * @param isSendBotMessage + * the isSendBotMessage to set + */ + public void setIsSendBotMessage(boolean isSendBotMessage){ + this.isSendBotMessage = isSendBotMessage; + } + +} diff --git a/feilong-net/feilong-net-bot/feilong-net-bot-api/src/main/java/com/feilong/net/bot/LogAndBotCombination.java b/feilong-net/feilong-net-bot/feilong-net-bot-api/src/main/java/com/feilong/net/bot/LogAndBotCombination.java index 3c96f778..a555bb1b 100644 --- a/feilong-net/feilong-net-bot/feilong-net-bot-api/src/main/java/com/feilong/net/bot/LogAndBotCombination.java +++ b/feilong-net/feilong-net-bot/feilong-net-bot-api/src/main/java/com/feilong/net/bot/LogAndBotCombination.java @@ -22,6 +22,7 @@ import org.slf4j.Logger; +import com.feilong.core.Validate; import com.feilong.core.lang.ClassUtil; import com.feilong.lib.lang3.ArrayUtils; import com.feilong.lib.lang3.StringUtils; @@ -109,6 +110,92 @@ public static void error(Logger logger,Bot bot,String pattern,Object...arguments //--------------------------------------------------------------- + /** + * Debug级别输出日志以及发送机器人. + * + * @param combinationConfig + * 混合相关配置 + * @param pattern + * the pattern + * @param arguments + * the arguments + * @since 4.1.0 + */ + public static void debug(CombinationConfig combinationConfig,String pattern,Object...arguments){ + Validate.notNull(combinationConfig, "combinationConfig can't be null!"); + debug( + combinationConfig.getIsPrintLog() ? combinationConfig.getLogger() : null, + combinationConfig.getIsSendBotMessage() ? combinationConfig.getBot() : null, + + joinPattern(combinationConfig.getSignName(), pattern), + arguments); + } + + /** + * Info级别输出日志以及发送机器人. + * + * @param combinationConfig + * 混合相关配置 + * @param pattern + * the pattern + * @param arguments + * the arguments + * @since 4.1.0 + */ + public static void info(CombinationConfig combinationConfig,String pattern,Object...arguments){ + Validate.notNull(combinationConfig, "combinationConfig can't be null!"); + info( + combinationConfig.getIsPrintLog() ? combinationConfig.getLogger() : null, + combinationConfig.getIsSendBotMessage() ? combinationConfig.getBot() : null, + + joinPattern(combinationConfig.getSignName(), pattern), + arguments); + } + + /** + * Warn级别输出日志以及发送机器人. + * + * @param combinationConfig + * 混合相关配置 + * @param pattern + * the pattern + * @param arguments + * the arguments + * @since 4.1.0 + */ + public static void warn(CombinationConfig combinationConfig,String pattern,Object...arguments){ + Validate.notNull(combinationConfig, "combinationConfig can't be null!"); + warn( + combinationConfig.getIsPrintLog() ? combinationConfig.getLogger() : null, + combinationConfig.getIsSendBotMessage() ? combinationConfig.getBot() : null, + + joinPattern(combinationConfig.getSignName(), pattern), + arguments); + } + + /** + * Error级别输出日志以及发送机器人. + * + * @param combinationConfig + * 混合相关配置 + * @param pattern + * the pattern + * @param arguments + * the arguments + * @since 4.1.0 + */ + public static void error(CombinationConfig combinationConfig,String pattern,Object...arguments){ + Validate.notNull(combinationConfig, "combinationConfig can't be null!"); + error( + combinationConfig.getIsPrintLog() ? combinationConfig.getLogger() : null, + combinationConfig.getIsSendBotMessage() ? combinationConfig.getBot() : null, + + joinPattern(combinationConfig.getSignName(), pattern), + arguments); + } + + //--------------------------------------------------------------- + /** * Debug级别输出日志以及发送机器人. * @@ -307,11 +394,13 @@ private static void log(Logger logger,String type,String pattern,Object...argume } /** - * 剔除最后一个参数 format - * + * 剔除最后一个参数 format. + * * @param pattern + * the pattern * @param arguments - * @return + * the arguments + * @return the string * @since 4.0.3 */ private static String formatPatternExcludeLastArgument(String pattern,Object...arguments){ @@ -319,6 +408,16 @@ private static String formatPatternExcludeLastArgument(String pattern,Object...a return formatPattern(pattern, ArrayUtils.subarray(arguments, 0, arguments.length - 1)); } + /** + * Log data. + * + * @param logger + * the logger + * @param type + * the type + * @param message + * the message + */ private static void logData(Logger logger,String type,String message){ if (Objects.equals(type, "debug")){ logger.debug(message);