Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Log4j commands

Options
  • 06-03-2006 11:08am
    #1
    Closed Accounts Posts: 4,237 ✭✭✭


    Is anyone here familiar with log4j and configuring it to extract the data you want?

    If so, I am almost there with my switchs and that and have my log file looking exactly as I want it to. I am trying to get the host name appear in the logs but have not found any commands to do this. I tried inputting uname -n but it does take and mearly inputs that to the log file. Any ideas?

    Oh its solaris its running under.


Comments

  • Registered Users Posts: 2,426 ✭✭✭ressem


    Not quite clear on what you're doing here?

    You can't use


    mylogger.info(java.net.InetAddress.getLocalHost().getHostName())

    To add the hostname to the log using your java code?

    Or are you trying to modify the log4j log file using Unix shell scripting only for some reason?


  • Closed Accounts Posts: 4,237 ✭✭✭iregk


    im just using the java properties files that sits in the classes directory.

    maybe that string you just gave will work.


  • Registered Users Posts: 2,426 ✭✭✭ressem


    Oh right, you are trying to modify the log4j properties file and looking for an appender that will add the hostname, to each line perhaps?

    Don't know of any, and can't see anything under the 3rd party extensions listed on log4j site.

    Not so trivial way is creating your own appender.
    http://www.javaworld.com/javaworld/jw-12-2004/jw-1220-toolbox-p2.html, and adds another out.write, over the others to prepend the hostname.

    Don't know enough myself to know other options.


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    The pattern layout is responsible for formatting logging events. Implement your own version that appends the hostname to the pattern such as
        class MyPatternLayout extends PatternLayout
        {
        	public MyPatternLayout(String pattern)
        	{
        		super(pattern);
        		
        		String hostName = null;
        	
        		try
        		{
        			hostName  = java.net.InetAddress.getLocalHost().getHostName();
        		}catch(UnknownHostException e)
        		{}
        		
        		if(hostName!=null)
        		{
        			setConversionPattern("("+hostName+") - " +pattern);
        			
        		}
        	}
        }
    

    and then in ur properties change the root layout to your implementation
    log4j.appender.ROOT.layout=com.abc.MyPatternLayout


  • Closed Accounts Posts: 4,237 ✭✭✭iregk


    ok i take it with this, im not simply putting this into the properties file but creating a pattern file with above code and then throwing the last line into the properties file?


  • Advertisement
Advertisement