`
johnhzjx
  • 浏览: 38413 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

LOG4J对单服务器多SERVER的集群服务的日志输出支持

阅读更多
增加LOG4J对单服务器多SERVER的集群服务的日志输出支持
问题:在单台物理服务器上部署的集群中含有多个Server Application时,发布的应用只有一个 WAR包,对应的LOG4J的配置也仅有一个,多个Server Application同时将输出的日志文件写入到一个文件中,会造成日志的丢失,因此设计该方法是为了多个Server Application分别输出各自的日志文件.

思路:
1.log4j的配置文件支持System Property的环境变量的获取,如user home
2.通过ServletContext可以获取到集群中每个server name
3.在不同的server启动时将不同server name的加载到System Property
4.在配置文件中增加一个${WebAppClusterServer}的参数,拼合server_name+logfile_name
5.配置文件的定义:
**************************************************
*logpath=e:/${WebSphereAppHost}_
*log4j.appender.logfile.File=${logpath}test.log
**************************************************

这是思路,需要源码的可以EMAIL我
源码是基于org.springframework.util.Log4jConfigurer进行改造

public class Log4jWebClusterConfig {

	/** Parameter specifying whether to expose the web app cluster property */
	public static final String EXPOSE_WEB_APP_ClUSTER_PARAM = "log4jExposeWebAppCluster";
	public static final String EXPOSE_WEB_APP_ClUSTER_ATTRIBUTE_PARAM = "log4jExposeWebAppClusterAttribute";

	public static void initWebClusterConfig(ServletContext servletContext) throws IllegalArgumentException {
		String webappcluster = servletContext.getInitParameter(EXPOSE_WEB_APP_ClUSTER_PARAM);
		String webappclusterattribute = servletContext.getInitParameter(EXPOSE_WEB_APP_ClUSTER_ATTRIBUTE_PARAM);
		if (webappcluster != null) {
			if (webappclusterattribute == null) {
				throw new IllegalArgumentException(
					"log4jExposeWebAppClusterAttribute is null ! "
						+ "  If set property 'log4jExposeWebAppCluster',"
						+ "'log4jExposeWebAppClusterAttribute' must not be null ");
			}
			String webappclusterattr = (String) servletContext.getAttribute(webappclusterattribute);
			if (webappclusterattr == null || webappclusterattr.equals("")) {
				throw new IllegalArgumentException(
					"Cannot get log4jExposeWebAppClusterAttribute=["
						+ webappclusterattribute
						+ "]"
						+ "  please reconfirm log4jExposeWebAppClusterAttribute's value!");
			}

			//only getAttribute for WebSphere,other WebContain need to reconfigure!!
			if (webappcluster.equals("WebSphere")) {
				System.setProperty("WebAppClusterServer", webappclusterattr);
			}
			servletContext.log(
				"Initialize Log4J For Web_App_Cluster=[" + webappcluster + "] Server=[" + webappclusterattr + "]");
		}

	}
	
	
//	getServletContext().log("----getAttributeNames----");
//	Enumeration vecKey = getServletContext().getAttributeNames();
//	while (vecKey.hasMoreElements()) {
//		String strKey = (String) vecKey.nextElement();
//		getServletContext().log("Key=[" + strKey + "] Value=[" + getServletContext().getAttribute(strKey) + "]");
//	}
//		
//	getServletContext().log("----System.getProperties----");
//	vecKey = propertiesInst.propertyNames();
//	while (vecKey.hasMoreElements()) {
//		String strKey = (String) vecKey.nextElement();
//		getServletContext().log("Key=[" + strKey + "] Value=[" + propertiesInst.getProperty(strKey) + "]");
//	}
//		
//		
//	getServletContext().log("----getInitParameterNames----");
//	vecKey = getServletContext().getInitParameterNames();
//	while (vecKey.hasMoreElements()) {
//		String strKey = (String) vecKey.nextElement();
//		getServletContext().log("Key=[" + strKey + "] Value=[" + getServletContext().getInitParameter(strKey) + "]");
//	}
//		
//	getServletContext().log("getServletContextName="+getServletContext().getServletContextName());

}



public abstract class Log4jWebConfigurer {

	/** Parameter specifying the location of the Log4J config file */
	public static final String CONFIG_LOCATION_PARAM = "log4jConfigLocation";

	/** Parameter specifying the refresh interval for checking the Log4J config file */
	public static final String REFRESH_INTERVAL_PARAM = "log4jRefreshInterval";

	/** Parameter specifying whether to expose the web app root system property */
	public static final String EXPOSE_WEB_APP_ROOT_PARAM = "log4jExposeWebAppRoot";

	/**
	 * Initialize Log4J, including setting the web app root system property.
	 * @param servletContext the current ServletContext
	 * @see WebUtils#setWebAppRootSystemProperty
	 */
	public static void initLogging(ServletContext servletContext) {
		// Expose the web app root system property.
		if (exposeWebAppRoot(servletContext)) {
			WebUtils.setWebAppRootSystemProperty(servletContext);
		}

		//Expose the web app cluster property.
		Log4jWebClusterConfig.initWebClusterConfig(servletContext);

		// Only perform custom Log4J initialization in case of a config file.
		String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
		if (location != null) {
			// Perform actual Log4J initialization; else rely on Log4J's default initialization.
			try {
				// Return a URL (e.g. "classpath:" or "file:") as-is;
				// consider a plain file path as relative to the web application root directory.
				if (!ResourceUtils.isUrl(location)) {
					// Resolve system property placeholders before resolving real path.
					location = SystemPropertyUtils.resolvePlaceholders(location);
					location = WebUtils.getRealPath(servletContext, location);
				}

				// Write log message to server log.
				servletContext.log("Initializing Log4J from [" + location + "]");

				// Check whether refresh interval was specified.
				String intervalString = servletContext.getInitParameter(REFRESH_INTERVAL_PARAM);
				if (intervalString != null) {
					// Initialize with refresh interval, i.e. with Log4J's watchdog thread,
					// checking the file in the background.
					try {
						long refreshInterval = Long.parseLong(intervalString);
						Log4jConfigurer.initLogging(location, refreshInterval);
					}
					catch (NumberFormatException ex) {
						throw new IllegalArgumentException("Invalid 'log4jRefreshInterval' parameter: " + ex.getMessage());
					}
				}
				else {
					// Initialize without refresh check, i.e. without Log4J's watchdog thread.
					Log4jConfigurer.initLogging(location);
				}
			}
			catch (FileNotFoundException ex) {
				throw new IllegalArgumentException("Invalid 'log4jConfigLocation' parameter: " + ex.getMessage());
			}
		}
	}

	/**
	 * Shut down Log4J, properly releasing all file locks
	 * and resetting the web app root system property.
	 * @param servletContext the current ServletContext
	 * @see WebUtils#removeWebAppRootSystemProperty
	 */
	public static void shutdownLogging(ServletContext servletContext) {
		servletContext.log("Shutting down Log4J");
		try {
			Log4jConfigurer.shutdownLogging();
		}
		finally {
			// Remove the web app root system property.
			if (exposeWebAppRoot(servletContext)) {
				WebUtils.removeWebAppRootSystemProperty(servletContext);
			}
		}
	}

	/**
	 * Return whether to expose the web app root system property,
	 * checking the corresponding ServletContext init parameter.
	 * @see #EXPOSE_WEB_APP_ROOT_PARAM
	 */
	private static boolean exposeWebAppRoot(ServletContext servletContext) {
		String exposeWebAppRootParam = servletContext.getInitParameter(EXPOSE_WEB_APP_ROOT_PARAM);
		return (exposeWebAppRootParam == null || Boolean.valueOf(exposeWebAppRootParam).booleanValue());
	}

}


   <context-param>
       <param-name>log4jExposeWebAppCluster</param-name>
       <param-value>WebSphere</param-value>
   </context-param>
   <context-param>
       <param-name>log4jExposeWebAppClusterAttribute</param-name>
       <param-value>com.ibm.websphere.servlet.application.host</param-value>
   </context-param>
	<servlet>
		<servlet-name>Log4jConfigServlet</servlet-name>
		<display-name>Log4jConfigServlet</display-name>
		<servlet-class>com.xxxxx.log4j.web.Log4jConfigServlet</servlet-class>
		<load-on-startup>4</load-on-startup>
	</servlet>



0
1
分享到:
评论
1 楼 sunman5277 2012-07-10  
我在部署weblogic集群时也遇到和你相同的问题,1个服务器部署了4个server,请问你具体是怎么解决的?望不吝赐教

相关推荐

    服务器基础知识介绍(服务器全部组件).pdf

    故障日志记录和 SNMP 警报发送 访问系统事件日志 (System Event Log ,SEL) 和传感器状况 控制包括开机和关机 独立于系统电源或工作状态的支持 模拟KVM 通过远程连接服务器的BMC端口,来实现对服务器的操作和控制,就...

    java查看sun包源码-LogWeb:高性能在线记录日志系统(支持集群管理)

    高性能在线记录日志系统(支持集群管理) 初始数据库:日志系统在设计初就考虑到了方便部署的方案。可分为三步。 第一步:配置好数据库连接信息(db.DB.config.xml),只须一个空的数据库即可。 第二步:运行日志服务...

    远程服务器跑matlab代码-GI-mathematical-modelling:GI数学建模

    远程服务器运行matlab代码GI数学建模 作者:林: 工程科学系奥克兰大学创建日期:2020年11月1日上次修改时间:2020年11月3日 该文件夹包含用于项目37:了解微观时空活动的所有脚本,代码和数据。 在继续任何工作之前...

    PowerJob-其他

    有需要全部机器一同执行的业务场景:如使用广播执行模式清理集群日志。 有需要分布式处理的业务场景:比如需要更新一大批数据,单机执行耗时非常长,可以使用Map/MapReduce处理器完成任务的分发,调动整个集群加速...

    jboss配置指南

    JBoss 的目录结构说明 目录 描述 bin 启动和关闭 JBoss 的脚本( run.bat 为 windows 系统下的启动脚本,shutdown.bat 为 windows ...server/default/log JBoss 的日志文件。 server/default/tmp JBoss 的临时文件。

    fourinone-3.04.25

    3、一次性启动多工人进程支持,可以通过程序api一次性启动和管理“ParkServer/工头/工人”多个进程,并附带良好的日志输出功能,用于代替写批处理脚本方式,方便部署和运行。 4、增加了相应指南和demo。 本软件...

    Oracle9i的init.ora参数中文说明

    值范围: 任何单字节字符, '+', '-', ', '&gt;' 除外。 默认值: 从 NLS_TERRITORY 中获得 nls_sort: 说明: 指定 ORDER BY 查询的比较顺序。对于二进制排序, ORDER BY 查询的比较顺序是以数值为基础的。对于语言排序, 则...

    Spring Cloud Finchley SR2全套(集成Spring Gateway)

    &lt;artifactId&gt;log4j-slf4j-impl Redis二次封装的这个项目主要实现了自动延期的功能,可以在配置的时候设置某些缓存是否需要自动延期&lt;默认为ture&gt;,自动延期的将会在获取的时候重置 过期时间来达到自动延期功能。...

    单点登录源码

    Log4J | 日志组件 | [http://logging.apache.org/log4j/1.2/](http://logging.apache.org/log4j/1.2/) Swagger2 | 接口测试框架 | [http://swagger.io/](http://swagger.io/) sequence | 分布式高效ID生产 | ...

    Jetty中文手册

    Maven和Ant的更多支持 Jetty Maven插件(Plugin) Jetty Jspc Maven插件(Plugin) Maven web应用工程原型 Ant Jetty插件(Plugin) 使用集成开发环境(IDEs) 在Eclipse中使用Jetty 在IntelliJ中使用Jetty 在...

    LambdaProbe 中文包下载

    支持的Tomcat 服务器版本: 5.0, 5.5, 6.0. 当然还有一个地方需要设置, 请在 conf/tomcat-users.xml 中添加 manager 账户, probe 需要这个账户才能正确登录使用. 如果不知道怎么做, 你可以把文件内容改成这样即可...

    网络架构师148讲视频课程

    │ 第80节:多线程consumer访问集群.avi │ 第81节:集群下的消息回流功能.avi │ 第82节:容错的链接和动态网络连接.avi │ 第83节:ActiveMQ的集群.avi │ 第84节:Destination高级特性一.avi │ 第85节:...

Global site tag (gtag.js) - Google Analytics