WindChat/windchat-common/src/main/java/com/windchat/common/logs/LogUtils.java

87 lines
3.7 KiB
Java
Raw Normal View History

2019-07-29 23:42:16 +08:00
/**
* Copyright 2018-2028 Akaxin Group
*
* 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.
*/
2019-11-27 23:33:33 +08:00
package com.windchat.common.logs;
2019-07-29 23:42:16 +08:00
2019-11-27 23:33:33 +08:00
import com.windchat.common.command.Command;
import com.windchat.common.command.CommandResponse;
import com.windchat.common.utils.StringHelper;
2019-07-29 23:42:16 +08:00
import org.slf4j.Logger;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MessageFormatter;
2019-11-27 23:36:54 +08:00
import com.windchat.common.command.Command;
import com.windchat.common.command.CommandResponse;
import com.windchat.common.utils.StringHelper;
2019-07-29 23:42:16 +08:00
/**
* 封装log针对网络日志以及数据库操作日志
*
* @author Sam{@link an.guoyue254@gmail.com}
* @since 2018-01-25 16:13:00
*/
public class LogUtils extends LogCreater {
public static void requestInfoLog(Logger logger, Command command, String messagePattern, Object... objects) {
FormattingTuple format = MessageFormatter.arrayFormat(messagePattern, objects);
logger.info("client={} siteUserId={} action={} msg={}", command.getClientIp(), command.getSiteUserId(),
command.getAction(), command.toString(), format.getMessage());
return;
}
public static void requestDebugLog(Logger logger, Command command, String requestStr) {
logger.debug("client={} siteUserId={} action={} command={} request={}", command.getClientIp(),
command.getSiteUserId(), command.getAction(), command.toString(), requestStr);
}
public static void requestErrorLog(Logger logger, Command command, Throwable t) {
logger.error(StringHelper.format("client={} siteUserId={} action={} uri={} error", command.getClientIp(),
command.getSiteUserId(), command.getAction(), command.getUri()), t);
}
public static void requestErrorLog(Logger logger, Command command, Class<?> clazz, Throwable t) {
logger.error(StringHelper.format("client={} siteUserId={} action={} uri={} {} error", command.getClientIp(),
command.getSiteUserId(), command.getAction(), command.getUri(), clazz.getClass().getName()), t);
}
public static void requestResultLog(Logger logger, Command command, CommandResponse response) {
try {
logger.info("client={} clientVersion={} siteUserId={} action={} uri={} cost={}ms result=[{}]",
command.getClientIp(), command.getClientVersion(), command.getSiteUserId(), command.getAction(),
command.getUri(), System.currentTimeMillis() - command.getStartTime(), response.getErrorCodeInfo());
} catch (Exception e) {
logger.error(StringHelper.format("request result log error command={} response={}", command, response), e);
}
}
public static void dbDebugLog(Logger logger, long startTime, Object result, String sql, Object... objects) {
String messagePattern = sql.replace("?", "{}");
FormattingTuple format = MessageFormatter.arrayFormat(messagePattern, objects);
2019-12-31 17:28:53 +08:00
logger.debug("[windchat-db] cost:{}ms result:{} sql:{}", System.currentTimeMillis() - startTime, result,
2019-07-29 23:42:16 +08:00
format.getMessage());
}
public static void info(org.apache.log4j.Logger logger, String messagePattern, Object object) {
FormattingTuple format = MessageFormatter.format(messagePattern, object);
logger.info(format.getMessage());
}
public static void info(org.apache.log4j.Logger logger, String messagePattern, Object... objects) {
FormattingTuple format = MessageFormatter.arrayFormat(messagePattern, objects);
logger.info(format.getMessage());
}
}