User: oberg
Date: 00/11/07 04:26:20
Modified: src/main/org/jboss/logging FileLogging.java
Log:
Now uses ServiceMBeanSupport. Cleeeaner
Allow user to set JMX ObjectName, but has decent default
Changed file handling
Revision Changes Path
1.8 +43 -38 jboss/src/main/org/jboss/logging/FileLogging.java
Index: FileLogging.java
===================================================================
RCS file: /products/cvs/ejboss/jboss/src/main/org/jboss/logging/FileLogging.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- FileLogging.java 2000/10/17 15:39:49 1.7
+++ FileLogging.java 2000/11/07 12:26:19 1.8
@@ -16,17 +16,19 @@
import java.util.StringTokenizer;
import javax.management.*;
+import org.jboss.util.ServiceMBeanSupport;
+
/**
*
* @see <related>
* @author Rickard �berg ([EMAIL PROTECTED])
- * @version $Revision: 1.7 $
+ * @version $Revision: 1.8 $
*/
public class FileLogging
+ extends ServiceMBeanSupport
implements FileLoggingMBean, MBeanRegistration, NotificationListener
{
// Constants -----------------------------------------------------
- public static final String OBJECT_NAME =
"DefaultDomain:service=Logging,type=File";
// Attributes ----------------------------------------------------
PrintStream out, err;
@@ -35,12 +37,13 @@
boolean verbose = false;
- Log log = new Log("File logging");
-
String filter = "Information,Debug,Warning,Error";
- String logName = "server.log";
+ String logName = "server";
String sources;
boolean append;
+
+ ObjectName name;
+ MBeanServer server;
// Static --------------------------------------------------------
@@ -82,16 +85,20 @@
this.format = format;
msgFmt = new MessageFormat(format);
}
+
public String getFormat() { return format; }
public void setLogName(String logName) throws FileNotFoundException
{
- if(!logName.equals(this.logName)) {
+ if(!logName.equals(this.logName))
+ {
this.logName = logName;
if (out != null)
+ {
out = null;
- openLogFile();
+ openLogFile();
+ }
}
}
public String getLogName() { return logName; }
@@ -100,6 +107,8 @@
public void handleNotification(Notification n,
java.lang.Object handback)
{
+ if (out == null) return; // Not started yet
+
if (sources == null || sources.length() == 0 ||
sources.indexOf(n.getUserData().toString()) != -1)
{
if (filter.indexOf(n.getType()) != -1)
@@ -110,48 +119,44 @@
}
}
- // MBeanRegistration implementation ------------------------------
- public ObjectName preRegister(MBeanServer server, ObjectName name)
- throws java.lang.Exception
+ // ServiceMBeanSupport implementation ----------------------------
+ public ObjectName getObjectName(MBeanServer server, ObjectName name)
+ throws javax.management.MalformedObjectNameException
{
- String objectName;
-
- objectName = OBJECT_NAME + ",sources=" + (sources == null ? "All" : sources);
- try
- {
- openLogFile();
- server.addNotificationListener(new
ObjectName(server.getDefaultDomain(),"service","Log"),this,null,null);
-
- log.log("Logging started");
- return new ObjectName(objectName);
-
- } catch (Throwable e)
- {
- Logger.exception(e);
- }
- return new ObjectName(objectName);
+ this.server = server;
+ this.name = name == null ? new ObjectName(OBJECT_NAME) : name;
+ return this.name;
}
-
- public void postRegister(java.lang.Boolean registrationDone)
+
+ public String getName()
{
+ return "File logging";
}
-
- public void preDeregister()
+
+ public void initService()
throws java.lang.Exception
- {}
+ {
+ String objectName;
- public void postDeregister() {}
+ openLogFile();
+ server.addNotificationListener(new
ObjectName(server.getDefaultDomain(),"service","Log"),this,null,null);
+ }
- // Private --------------------------------------------------
- private void openLogFile() throws FileNotFoundException {
+ // Private -------------------------------------------------------
+ private void openLogFile()
+ throws FileNotFoundException
+ {
URL properties = getClass().getResource("/log.properties");
if(properties == null)
- System.err.println("Unable to identify logging directory!");
+ throw new FileNotFoundException("Unable to identify logging directory!");
+
File parent = new File(properties.getFile()).getParentFile();
- File logFile = new File(parent, logName);
- try {
+ File logFile = new File(parent, logName+".log");
+ try
+ {
out = new PrintStream(new FileOutputStream(logFile.getCanonicalPath(),
append));
- } catch (IOException e) {
+ } catch (IOException e)
+ {
throw new FileNotFoundException(e.getMessage());
}
}