From 62dda09c37ee47a35c98a500a2563f8279fb4ac8 Mon Sep 17 00:00:00 2001 From: richie Date: Fri, 16 Aug 2019 11:01:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E7=9B=B8=E5=85=B3=E7=9A=84AP?= =?UTF-8?q?I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fanruan/api/cal/ParameterKit.java | 13 ++ src/main/java/com/fanruan/api/log/LogKit.java | 163 ++++++++++++++++++ .../com/fanruan/api/cal/ParameterKitTest.java | 23 +++ .../java/com/fanruan/api/log/LogKitTest.java | 26 +++ 4 files changed, 225 insertions(+) create mode 100644 src/main/java/com/fanruan/api/log/LogKit.java create mode 100644 src/test/java/com/fanruan/api/log/LogKitTest.java diff --git a/src/main/java/com/fanruan/api/cal/ParameterKit.java b/src/main/java/com/fanruan/api/cal/ParameterKit.java index 4bc9820..430986f 100644 --- a/src/main/java/com/fanruan/api/cal/ParameterKit.java +++ b/src/main/java/com/fanruan/api/cal/ParameterKit.java @@ -1,7 +1,11 @@ package com.fanruan.api.cal; import com.fr.base.ParameterHelper; +import com.fr.base.ParameterMapNameSpace; import com.fr.stable.ParameterProvider; +import com.fr.stable.script.NameSpace; + +import java.util.Map; /** * @author richie @@ -19,4 +23,13 @@ public class ParameterKit { public static ParameterProvider[] analyze4ParametersFromFormula(String text) { return ParameterHelper.analyze4ParametersFromFormula(text); } + + /** + * 创建一个用于计算的参数对名字空间 + * @param map 参数键值对 + * @return 名字空间,用于传递给算子做计算 + */ + public static NameSpace createParameterMapNameSpace(Map map) { + return ParameterMapNameSpace.create(map); + } } diff --git a/src/main/java/com/fanruan/api/log/LogKit.java b/src/main/java/com/fanruan/api/log/LogKit.java new file mode 100644 index 0000000..bc5fc19 --- /dev/null +++ b/src/main/java/com/fanruan/api/log/LogKit.java @@ -0,0 +1,163 @@ +package com.fanruan.api.log; + +import com.fr.log.FineLoggerFactory; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-16 + * 写日志的工具类 + */ +public class LogKit { + + /** + * 输出SQL日志,在日志中会有前缀[SQL] + * + * @param sql sql内容 + */ + public static void sql(String sql) { + FineLoggerFactory.getLogger().sql(sql); + } + + /** + * 是否启用了debug级别的日志输出 + * + * @return 启用了debug级别的日志输出则返回true,否则返回false + */ + public static boolean isDebugEnabled() { + return FineLoggerFactory.getLogger().isDebugEnabled(); + } + + /** + * 输出debug级别的日志信息 + * + * @param msg 信息 + */ + public static void debug(String msg) { + FineLoggerFactory.getLogger().debug(msg); + } + + /** + * 输出debug级别的日志信息 + * + * @param format 日志信息模板,形如: "hello, LiLei, I am {}." + * @param args 模板中的参数,和{}出现的顺序对应 + */ + public static void debug(String format, Object... args) { + FineLoggerFactory.getLogger().debug(format, args); + } + + /** + * 输出debug级别的日志信息 + * @param msg 日志信息 + * @param e 异常对象 + */ + public static void debug(String msg, Throwable e) { + FineLoggerFactory.getLogger().debug(msg, e); + } + + /** + * 是否启用了info级别的日志输出 + * + * @return 启用了info级别的日志输出则返回true,否则返回false + */ + public static boolean isInfoEnabled() { + return FineLoggerFactory.getLogger().isInfoEnabled(); + } + + /** + * 输出info级别的日志信息 + * + * @param msg 信息 + */ + public static void info(String msg) { + FineLoggerFactory.getLogger().info(msg); + } + + /** + * 输出info级别的日志信息 + * + * @param format 日志信息模板,形如: "hello, LiLei, I am {}." + * @param args 模板中的参数,和{}出现的顺序对应 + */ + public static void info(String format, Object... args) { + FineLoggerFactory.getLogger().info(format, args); + } + + /** + * 输出warn级别的日志信息 + * + * @param msg 信息 + */ + public static void warn(String msg) { + FineLoggerFactory.getLogger().warn(msg); + } + + /** + * 输出warn级别的日志信息 + * + * @param format 日志信息模板,形如: "hello, LiLei, I am {}." + * @param args 模板中的参数,和{}出现的顺序对应 + */ + public static void warn(String format, Object... args) { + FineLoggerFactory.getLogger().warn(format, args); + } + + /** + * 输出warn级别的日志信息 + * @param msg 日志信息 + * @param e 异常对象 + */ + public static void warn(String msg, Throwable e) { + FineLoggerFactory.getLogger().warn(msg, e); + } + + /** + * 输出warn级别的日志信息 + * @param e 异常对象 + * @param format 日志信息模板,形如: "hello, LiLei, I am {}." + * @param args 模板中的参数,和{}出现的顺序对应 + */ + public static void warn(Throwable e, String format, Object... args) { + FineLoggerFactory.getLogger().warn(e, format, args); + } + + /** + * 输出error级别信息 + * + * @param msg 信息 + */ + public static void error(String msg) { + FineLoggerFactory.getLogger().error(msg); + } + + /** + * 输出error级别的日志信息 + * + * @param format 日志信息模板,形如: "hello, LiLei, I am {}." + * @param args 模板中的参数,和{}出现的顺序对应 + */ + public static void error(String format, Object... args) { + FineLoggerFactory.getLogger().error(format, args); + } + + /** + * 输出error级别信息 + * + * @param msg 信息 + * @param e 异常 + */ + public static void error(String msg, Throwable e) { + FineLoggerFactory.getLogger().error(msg, e); + } + + /** + * 输出error级别的日志信息 + * @param e 异常对象 + * @param format 日志信息模板,形如: "hello, LiLei, I am {}." + * @param args 模板中的参数,和{}出现的顺序对应 + */ + public static void error(Throwable e, String format, Object... args) { + FineLoggerFactory.getLogger().error(e, format, args); + } +} diff --git a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java index e37126c..20bb157 100644 --- a/src/test/java/com/fanruan/api/cal/ParameterKitTest.java +++ b/src/test/java/com/fanruan/api/cal/ParameterKitTest.java @@ -1,10 +1,15 @@ package com.fanruan.api.cal; import com.fanruan.api.Prepare; +import com.fr.script.Calculator; import com.fr.stable.ParameterProvider; +import com.fr.stable.UtilEvalError; +import com.fr.stable.script.NameSpace; import org.junit.Assert; import org.junit.Test; +import java.util.HashMap; +import java.util.Map; import java.util.TreeMap; /** @@ -24,4 +29,22 @@ public class ParameterKitTest extends Prepare { String[] names = map.keySet().toArray(new String[0]); Assert.assertArrayEquals(new String[]{"p1", "p2", "p4", "p5"}, names); } + + @Test + public void createParameterMapNameSpace() { + Map map = new HashMap<>(); + map.put("p1", 100); + map.put("p2", 200); + NameSpace ns = ParameterKit.createParameterMapNameSpace(map); + Calculator calculator = Calculator.createCalculator(); + calculator.pushNameSpace(ns); + try { + Object r = calculator.eval("=$p1 + $p2"); + Assert.assertEquals(300, r); + } catch (UtilEvalError utilEvalError) { + Assert.fail(); + } finally { + calculator.removeNameSpace(ns); + } + } } \ No newline at end of file diff --git a/src/test/java/com/fanruan/api/log/LogKitTest.java b/src/test/java/com/fanruan/api/log/LogKitTest.java new file mode 100644 index 0000000..f0cb25f --- /dev/null +++ b/src/test/java/com/fanruan/api/log/LogKitTest.java @@ -0,0 +1,26 @@ +package com.fanruan.api.log; + +import com.fanruan.api.Prepare; +import com.fr.general.FRLogger; +import com.fr.general.log.Log4jConfig; +import com.fr.log.FineLoggerFactory; +import com.fr.third.apache.log4j.Level; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-08-16 + */ +public class LogKitTest extends Prepare { + + @Test + public void isDebugEnabled() { + Log4jConfig.getInstance().setRootLevel(Level.DEBUG); + FineLoggerFactory.registerLogger(FRLogger.getLogger()); + Assert.assertTrue(LogKit.isDebugEnabled()); + Assert.assertTrue(LogKit.isInfoEnabled()); + } + +} \ No newline at end of file