Skip to content

Commit

Permalink
新建com.feilong.net.bot.LogAndBotCombination.error(CombinationConfig,
Browse files Browse the repository at this point in the history
String, Object...) fix #712

新建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
  • Loading branch information
venusdrogon committed Jun 7, 2024
1 parent e73326a commit df2048e
Show file tree
Hide file tree
Showing 2 changed files with 328 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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 <a href="https://github.com/ifeilong/feilong">feilong</a>
* @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 那么不发送消息.
*
* <p>
* <b>场景:</b> 原先有相关代码发送了消息, 后来需要加个逻辑, 判定部分id不发送 可以把这个值设置为false
* </p>
*/
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 那么不发送消息.
*
* <p>
* <b>场景:</b> 原先有相关代码发送了消息, 后来需要加个逻辑, 判定部分id不发送 可以把这个值设置为false
* </p>
*/
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 那么不发送消息.
*
* <p>
* <b>场景:</b> 原先有相关代码发送了消息, 后来需要加个逻辑, 判定部分id不发送 可以把这个值设置为false
* </p>
*/
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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级别输出日志以及发送机器人.
*
Expand Down Expand Up @@ -307,18 +394,30 @@ 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){
//起始索引包含,结束索引不包含。空数组输入产生空输出。
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);
Expand Down

0 comments on commit df2048e

Please sign in to comment.