1 13 14 package org.ejbca.core.model.log; 15 16 import java.io.Serializable ; 17 import java.security.cert.X509Certificate ; 18 import java.text.DateFormat ; 19 import java.util.Date ; 20 import java.util.Properties ; 21 22 import org.apache.log4j.Level; 23 import org.apache.log4j.Logger; 24 import org.apache.log4j.Priority; 25 import org.ejbca.core.model.InternalResources; 26 27 28 33 public class Log4jLogDevice implements ILogDevice, Serializable { 34 35 36 private static final Logger log = Logger.getLogger(Log4jLogDevice.class); 37 38 39 private static final InternalResources intres = InternalResources.getInstance(); 40 41 44 private static Log4jLogDevice instance; 45 46 47 52 53 protected Log4jLogDevice(Properties prop) throws Exception { 54 } 56 57 63 public static synchronized ILogDevice instance(Properties prop) throws Exception { 64 if (instance == null) { 65 instance = new Log4jLogDevice(prop); 66 } 67 return instance; 68 } 69 70 public void log(Admin admininfo, int caid, int module, Date time, String username, X509Certificate certificate, int event, String comment) { 71 log(admininfo, caid, module, time, username, certificate, event, comment, null); 72 } 73 74 public void log(Admin admininfo, int caid, int module, Date time, String username, X509Certificate certificate, int event, String comment, Exception exception) { 75 76 String user = intres.getLocalizedMessage("log.nouserinvolved"); 77 String cert = intres.getLocalizedMessage("log.nocertinvolved"); 78 String admin = intres.getLocalizedMessage("log.adminnotknown"); 79 80 if (username != null) { 81 user = username; 82 } 83 84 if (certificate != null) { 85 cert = certificate.getSerialNumber().toString(16) + ", issuer: " + certificate.getIssuerDN().toString(); 86 } 87 88 if (admininfo.getAdminType() == Admin.TYPE_CLIENTCERT_USER) { 89 admin = Admin.ADMINTYPETEXTS[Admin.TYPE_CLIENTCERT_USER] + ", Certificate SNR : " + admininfo.getAdminData(); 90 } else if (admininfo.getAdminType() == Admin.TYPE_PUBLIC_WEB_USER) { 91 if (admininfo.getAdminData() != null) { 92 if (!admininfo.getAdminData().equals("")) 93 admin = Admin.ADMINTYPETEXTS[Admin.TYPE_PUBLIC_WEB_USER] + ", IP Address : " + admininfo.getAdminData(); 94 } else { 95 admin = Admin.ADMINTYPETEXTS[Admin.TYPE_PUBLIC_WEB_USER]; 96 } 97 } else { 98 admin = Admin.ADMINTYPETEXTS[admininfo.getAdminType()]; 99 } 100 101 Priority priority = Level.INFO; 102 String eventText = ""; 103 if (event >= LogEntry.EVENT_ERROR_BOUNDRARY) { 104 priority = Level.ERROR; 105 event -= LogEntry.EVENT_ERROR_BOUNDRARY; 106 eventText = LogEntry.EVENTNAMES_ERROR[event]; 107 }else{ 108 eventText = LogEntry.EVENTNAMES_INFO[event]; 109 } 110 111 String logline = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(time) + ", CAId : " + caid + ", " + LogEntry.MODULETEXTS[module] + ", " + eventText + ", Administrator : " + 112 admin + ", User : " + user + ", Certificate : " + cert + ", Comment : " + comment; 113 log.log(priority, logline, null); 114 115 if (exception != null) { 116 log.error("Exception : ", exception); 117 } 118 } 119 } 120 | Popular Tags |