├── README.md ├── pom.xml └── src └── main ├── java └── com │ └── cloudwise │ └── syslog4j │ ├── annotation │ └── EnableSyslog.java │ ├── autoconfig │ └── SyslogAutoconfig.java │ ├── condition │ └── OnAnnotationCondition.java │ ├── handler │ └── SyslogServerhandler.java │ ├── properties │ └── SyslogProperties.java │ ├── runable │ └── SyslogRunable.java │ └── store │ └── SyslogServerStore.java └── resources └── META-INF └── spring.factories /README.md: -------------------------------------------------------------------------------- 1 | # syslog 2 | syslog-spring-boot-starter 3 | 本项目封装syslog,让使用者只需要在配置文件中添加相关配置 即可开启syslogserver(服务端/接收端) 4 | 5 | - maven依赖 6 | ```pom 7 | 8 | com.cloudwise 9 | syslog-spring-boot-starter 10 | 0.0.1-SNAPSHOT 11 | 12 | ``` 13 | - 配置文件 14 | ```yaml 15 | cloudwise: 16 | syslog: 17 | #enable: false 18 | server: 19 | - host: 0.0.0.0 20 | port: 1231 21 | protocol: UDP 22 | handlerClass: org.productivity.java.syslog4j.server.impl.event.printstream.SystemErrSyslogServerEventHandler 23 | - host: 0.0.0.0 24 | port: 1121 25 | protocol: UDP 26 | handlerClass: org.productivity.java.syslog4j.server.impl.event.printstream.SystemErrSyslogServerEventHandler 27 | - host: 0.0.0.0 28 | port: 7777 29 | protocol: UDP 30 | handlerClass: org.productivity.java.syslog4j.server.impl.event.printstream.SystemErrSyslogServerEventHandler 31 | 32 | ``` 33 | - 启动示例 34 | ![图片](https://user-images.githubusercontent.com/44597411/126607211-bfdba270-9de1-4e7b-ad26-ec75747a6cf6.png) 35 | 36 | ![图片](https://user-images.githubusercontent.com/44597411/126607104-1e5e3620-2053-4efe-9437-65d08238f5a6.png) 37 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.5.2 9 | 10 | 11 | com.cloudwise 12 | syslog-spring-boot-starter 13 | 0.0.1-SNAPSHOT 14 | syslog-spring-boot-starter 15 | jar 16 | syslog-spring-boot-starter 17 | 18 | 1.8 19 | 0.9.30 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-test 30 | test 31 | 32 | 33 | 34 | org.syslog4j 35 | syslog4j 36 | ${syslog.version} 37 | 38 | 39 | 40 | org.projectlombok 41 | lombok 42 | true 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | maven-clean-plugin 52 | 3.1.0 53 | 54 | 55 | 56 | maven-resources-plugin 57 | 3.0.2 58 | 59 | 60 | maven-compiler-plugin 61 | 3.8.0 62 | 63 | 64 | maven-surefire-plugin 65 | 2.22.1 66 | 67 | 68 | maven-jar-plugin 69 | 3.0.2 70 | 71 | 72 | maven-install-plugin 73 | 2.5.2 74 | 75 | 76 | maven-deploy-plugin 77 | 2.8.2 78 | 79 | 80 | 81 | maven-site-plugin 82 | 3.7.1 83 | 84 | 85 | maven-project-info-reports-plugin 86 | 3.0.0 87 | 88 | 89 | 90 | 91 | 92 | org.apache.maven.plugins 93 | maven-compiler-plugin 94 | 95 | 8 96 | 8 97 | 98 | 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /src/main/java/com/cloudwise/syslog4j/annotation/EnableSyslog.java: -------------------------------------------------------------------------------- 1 | package com.cloudwise.syslog4j.annotation; 2 | 3 | import com.cloudwise.syslog4j.autoconfig.SyslogAutoconfig; 4 | import com.cloudwise.syslog4j.properties.SyslogProperties; 5 | import org.productivity.java.syslog4j.server.SyslogServer; 6 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 7 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 8 | import org.springframework.context.annotation.Import; 9 | 10 | import java.lang.annotation.*; 11 | 12 | /** 13 | * @author Aiden.Liu 14 | * @description syslog自动配置注解, 15 | * @date 2021-07-15 12:42 16 | */ 17 | @Target({ElementType.TYPE}) 18 | @Retention(RetentionPolicy.RUNTIME) 19 | @Documented 20 | @EnableConfigurationProperties(SyslogProperties.class) 21 | @ConditionalOnClass(SyslogServer.class) 22 | @Import({SyslogAutoconfig.class}) 23 | public @interface EnableSyslog { 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/cloudwise/syslog4j/autoconfig/SyslogAutoconfig.java: -------------------------------------------------------------------------------- 1 | package com.cloudwise.syslog4j.autoconfig; 2 | 3 | import com.cloudwise.syslog4j.condition.OnAnnotationCondition; 4 | import com.cloudwise.syslog4j.properties.SyslogProperties; 5 | import com.cloudwise.syslog4j.runable.SyslogRunable; 6 | import lombok.extern.java.Log; 7 | import org.springframework.beans.factory.InitializingBean; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 10 | import org.springframework.boot.context.properties.ConfigurationProperties; 11 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 12 | import org.springframework.context.ApplicationContext; 13 | import org.springframework.context.annotation.Conditional; 14 | import org.springframework.context.annotation.Configuration; 15 | import org.springframework.core.type.AnnotatedTypeMetadata; 16 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 17 | import org.springframework.util.StringUtils; 18 | 19 | import java.util.List; 20 | import java.util.Objects; 21 | import java.util.concurrent.*; 22 | import java.util.stream.Collectors; 23 | 24 | /** 25 | * @author Aiden.Liu 26 | * @description syslog自动配置 27 | * @date 2021-07-15 12:48 28 | */ 29 | @ConditionalOnProperty(value = "cloudwise.syslog.enable", havingValue = "true", matchIfMissing = true) 30 | @Configuration 31 | @EnableConfigurationProperties({SyslogProperties.class}) 32 | //@Conditional({OnAnnotationCondition.class}) 33 | @Log 34 | public class SyslogAutoconfig implements InitializingBean { 35 | 36 | @Autowired 37 | SyslogProperties syslogProperties; 38 | 39 | @Autowired 40 | ApplicationContext context; 41 | 42 | @Override 43 | public void afterPropertiesSet() { 44 | AnnotatedTypeMetadata metadata; 45 | if (Objects.isNull(syslogProperties) || Objects.isNull(syslogProperties.getServer())) { 46 | log.info(">>>>>syslogProperties is null, skipped the configure of syslog"); 47 | return; 48 | } 49 | List serverList = syslogProperties.getServer(); 50 | serverList = serverList.stream() 51 | .filter(p -> StringUtils.hasText(p.getHandlerClass())).distinct() 52 | .collect(Collectors.toList()); 53 | ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); 54 | //int i = Runtime.getRuntime().availableProcessors();//获取到服务器的cpu内核 55 | executor.setCorePoolSize(serverList.size());//核心池大小 56 | executor.setMaxPoolSize(100);//最大线程数 57 | executor.setQueueCapacity(1000);//队列程度 58 | executor.setKeepAliveSeconds(1000);//线程空闲时间 59 | executor.setThreadNamePrefix("syslog-therd");//线程前缀名称 60 | executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());//配置拒绝策略 61 | executor.initialize(); 62 | serverList.forEach(k -> { 63 | executor.execute(new SyslogRunable(k, context)); 64 | }); 65 | log.info(">>>>>syslog启动"); 66 | log.info(">>>>>有效启动参数:" + serverList); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/cloudwise/syslog4j/condition/OnAnnotationCondition.java: -------------------------------------------------------------------------------- 1 | package com.cloudwise.syslog4j.condition; 2 | 3 | import com.cloudwise.syslog4j.annotation.EnableSyslog; 4 | import org.springframework.boot.autoconfigure.condition.ConditionOutcome; 5 | import org.springframework.boot.autoconfigure.condition.SpringBootCondition; 6 | import org.springframework.context.annotation.ConditionContext; 7 | import org.springframework.core.annotation.AnnotationUtils; 8 | import org.springframework.core.type.AnnotatedTypeMetadata; 9 | 10 | import java.util.Map; 11 | 12 | public class OnAnnotationCondition extends SpringBootCondition { 13 | @Override 14 | public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { 15 | Map beansWithAnnotation = context.getBeanFactory().getBeansWithAnnotation(EnableSyslog.class); 16 | return new ConditionOutcome(!beansWithAnnotation.isEmpty(), "skipped the configure of syslog"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/cloudwise/syslog4j/handler/SyslogServerhandler.java: -------------------------------------------------------------------------------- 1 | package com.cloudwise.syslog4j.handler; 2 | 3 | import org.productivity.java.syslog4j.server.SyslogServerEventHandlerIF; 4 | import org.springframework.context.ApplicationContext; 5 | 6 | public interface SyslogServerhandler extends SyslogServerEventHandlerIF{ 7 | /** 8 | * init method 9 | * @param applicationContext 10 | */ 11 | default void init(ApplicationContext applicationContext){ 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/cloudwise/syslog4j/properties/SyslogProperties.java: -------------------------------------------------------------------------------- 1 | package com.cloudwise.syslog4j.properties; 2 | 3 | import lombok.Data; 4 | import lombok.Getter; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | 7 | import java.util.List; 8 | 9 | /** 10 | * @author Aiden.Liu 11 | * @description syslog配置文件 12 | * @date 2021-07-15 10:25 13 | */ 14 | @ConfigurationProperties(prefix = "cloudwise.syslog") 15 | @Data 16 | public class SyslogProperties { 17 | List server; 18 | 19 | @Getter 20 | public enum Protocol{ 21 | UDP("udp"),TCP("tcp"); 22 | String value; 23 | 24 | Protocol(String value) { 25 | this.value = value; 26 | } 27 | } 28 | 29 | @Data 30 | public static class SyslogConfig { 31 | String host; 32 | 33 | @Override 34 | public boolean equals(Object o) { 35 | if (this == o) return true; 36 | if (o == null || getClass() != o.getClass()) return false; 37 | 38 | SyslogConfig that = (SyslogConfig) o; 39 | 40 | if (port != that.port) return false; 41 | return host != null ? host.equals(that.host) : that.host == null; 42 | } 43 | 44 | @Override 45 | public int hashCode() { 46 | int result = host != null ? host.hashCode() : 0; 47 | result = 31 * result + port; 48 | return result; 49 | } 50 | 51 | int port; 52 | Protocol protocol; 53 | String handlerClass; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/cloudwise/syslog4j/runable/SyslogRunable.java: -------------------------------------------------------------------------------- 1 | package com.cloudwise.syslog4j.runable; 2 | 3 | import com.cloudwise.syslog4j.handler.SyslogServerhandler; 4 | import com.cloudwise.syslog4j.properties.SyslogProperties; 5 | import com.cloudwise.syslog4j.store.SyslogServerStore; 6 | import lombok.AllArgsConstructor; 7 | import lombok.NoArgsConstructor; 8 | import org.productivity.java.syslog4j.SyslogRuntimeException; 9 | import org.productivity.java.syslog4j.server.SyslogServerConfigIF; 10 | import org.productivity.java.syslog4j.server.SyslogServerEventHandlerIF; 11 | import org.productivity.java.syslog4j.server.SyslogServerEventIF; 12 | import org.productivity.java.syslog4j.server.SyslogServerIF; 13 | import org.productivity.java.syslog4j.server.impl.net.tcp.TCPNetSyslogServerConfig; 14 | import org.productivity.java.syslog4j.server.impl.net.udp.UDPNetSyslogServerConfig; 15 | import org.springframework.context.ApplicationContext; 16 | 17 | import java.util.Arrays; 18 | 19 | 20 | /** 21 | * @author Aiden.Liu 22 | * @description 23 | * @date 2021-07-15 12:54 24 | */ 25 | @AllArgsConstructor 26 | @NoArgsConstructor 27 | public class SyslogRunable implements Runnable { 28 | 29 | SyslogProperties.SyslogConfig syslogConfig; 30 | 31 | ApplicationContext context; 32 | 33 | SyslogServerConfigIF fillConfig(SyslogProperties.SyslogConfig syslogConfig) { 34 | SyslogServerConfigIF config; 35 | int port = syslogConfig.getPort(); 36 | String host = syslogConfig.getHost(); 37 | 38 | switch (syslogConfig.getProtocol()) { 39 | case TCP: 40 | config = new TCPNetSyslogServerConfig(host, port); 41 | break; 42 | case UDP: 43 | default: 44 | config = new UDPNetSyslogServerConfig(host, port); 45 | break; 46 | } 47 | return config; 48 | } 49 | 50 | @Override 51 | public void run() { 52 | SyslogServerConfigIF config = fillConfig(syslogConfig); 53 | String protocol = syslogConfig.getProtocol().getValue(); 54 | if (null != SyslogServerStore.get(config)) { 55 | return; 56 | } 57 | String handlerClassStr = syslogConfig.getHandlerClass(); 58 | if (null == handlerClassStr || "".equals(handlerClassStr)) { 59 | return; 60 | } else { 61 | try { 62 | Class handlerClass = Class.forName(handlerClassStr); 63 | Object handler = handlerClass.newInstance(); 64 | if (!(handler instanceof SyslogServerhandler)) { 65 | throw new RuntimeException("the handler must implements the interface that named SyslogServerEventHandlerIF"); 66 | } 67 | ((SyslogServerhandler)handler).init(context); 68 | config.addEventHandler((SyslogServerEventHandlerIF) handler); 69 | } catch (Exception e) { 70 | e.printStackTrace(); 71 | } 72 | } 73 | SyslogServerIF syslogServer = execSyslogServer(config, protocol); 74 | SyslogServerStore.put(config, syslogServer); 75 | } 76 | 77 | private SyslogServerIF execSyslogServer(SyslogServerConfigIF config, String protocol) { 78 | SyslogServerIF syslogServer; 79 | try { 80 | Class syslogClass = config.getSyslogServerClass(); 81 | syslogServer = (SyslogServerIF) syslogClass.newInstance(); 82 | } catch (ClassCastException var7) { 83 | throw new SyslogRuntimeException(var7); 84 | } catch (IllegalAccessException var8) { 85 | throw new SyslogRuntimeException(var8); 86 | } catch (InstantiationException var9) { 87 | throw new SyslogRuntimeException(var9); 88 | } 89 | syslogServer.initialize(protocol, config); 90 | 91 | if (syslogServer.getThread() == null) { 92 | Thread thread = new Thread(syslogServer); 93 | thread.setName("SyslogServer: " + config.getHost() + ":" + config.getPort() + "-" + protocol); 94 | syslogServer.setThread(thread); 95 | thread.start(); 96 | } 97 | return syslogServer; 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/com/cloudwise/syslog4j/store/SyslogServerStore.java: -------------------------------------------------------------------------------- 1 | package com.cloudwise.syslog4j.store; 2 | 3 | 4 | 5 | import org.productivity.java.syslog4j.impl.net.tcp.TCPNetSyslogConfig; 6 | import org.productivity.java.syslog4j.impl.net.udp.UDPNetSyslogConfig; 7 | import org.productivity.java.syslog4j.server.SyslogServerConfigIF; 8 | import org.productivity.java.syslog4j.server.SyslogServerIF; 9 | 10 | import java.util.Map; 11 | import java.util.concurrent.ConcurrentHashMap; 12 | 13 | /** 14 | * @author Aiden.Liu 15 | * @description 16 | * @date 2021-07-16 18:11 17 | */ 18 | public class SyslogServerStore { 19 | private static Map instances = new ConcurrentHashMap(16); 20 | 21 | public static void put(SyslogServerConfigIF config, SyslogServerIF syslogServer) { 22 | String key = grantKey(config); 23 | instances.put(key, syslogServer); 24 | } 25 | 26 | protected static String grantKey(SyslogServerConfigIF config) { 27 | String key = config.getHost() + "-" + config.getPort() + "-"; 28 | if (config instanceof UDPNetSyslogConfig) { 29 | key = key + "udp"; 30 | } else if (config instanceof TCPNetSyslogConfig) { 31 | key = key + "tcp"; 32 | } 33 | return key; 34 | } 35 | 36 | public static SyslogServerIF get(SyslogServerConfigIF config) { 37 | String key = grantKey(config); 38 | return (SyslogServerIF) instances.get(key); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/spring.factories: -------------------------------------------------------------------------------- 1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.cloudwise.syslog4j.autoconfig.SyslogAutoconfig --------------------------------------------------------------------------------