| | |
| | | package com.zy.core; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.lang.reflect.Array; |
| | |
| | | */ |
| | | @Slf4j |
| | | public class News { |
| | | |
| | | public static void main(String[] args) { |
| | | News.info("info{}", 1); |
| | | News.warn("warn{}", 2); |
| | | News.error("error{}", 3); |
| | | System.out.println(JSON.toJSONString(NEWS_QUEUE.data())); |
| | | } |
| | | |
| | | interface NewsSupport<T> { boolean execute(T t); } |
| | | |
| | |
| | | |
| | | } |
| | | |
| | | public static void info(String format) { |
| | | offer(NewsLevel.INFO, format, null); |
| | | } |
| | | |
| | | public static void info(String format, Object argument) { |
| | | offer(NewsLevel.INFO, format, new Object[]{argument}); |
| | | } |
| | | |
| | | public static void info(String format, Object... arguments) { |
| | | offer(NewsLevel.INFO, format, arguments); |
| | | } |
| | |
| | | } |
| | | |
| | | private static boolean offer(NewsLevel level, String msg, Object[] args) { |
| | | return NEWS_QUEUE.offer(new NewsDomain(level, msg, new Date())); |
| | | return NEWS_QUEUE.offer(new NewsDomain(level, replace(msg, args), new Date())); |
| | | } |
| | | |
| | | private static String replace(String str, Object[] objs){ |
| | | if (null == objs || objs.length == 0 || null == str || "".equals(str.trim())) { |
| | | return str; |
| | | } else { |
| | | StringBuilder sb = new StringBuilder(str); |
| | | for (Object obj : objs) { |
| | | int idx = sb.indexOf("{}"); |
| | | if (idx == -1) { break; } |
| | | sb.replace(idx, idx + 2, String.valueOf(obj)); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | } |
| | | |
| | | static class NewsDomain { |