01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.workflow.util;
06:
07: import com.opensymphony.module.propertyset.PropertySet;
08:
09: import com.opensymphony.util.TextUtils;
10:
11: import com.opensymphony.workflow.Register;
12: import com.opensymphony.workflow.WorkflowContext;
13: import com.opensymphony.workflow.spi.WorkflowEntry;
14:
15: import org.apache.commons.logging.Log;
16: import org.apache.commons.logging.LogFactory;
17:
18: import java.util.Map;
19:
20: /**
21: * This is a register, which helps logging using commons-logging.
22: * It wraps a Logger instance which is linked to the "OSWorkflow/<workflow_name>/<id>" category
23: *
24: * Following optional arguments available:
25: * <ul>
26: * <li>addInstanceId=true/false - if the instance id of the workflow should be added to the category. Defaults to false</li>
27: * <li>Category="OSWorkflow" - change the name of the log category other than "OSWorkflow"</li>
28: * </ul>
29: *
30: *
31: * If you register this class as "Logger", then you may use it from a Beanshell script like:
32: * <pre>
33: * logger = transientVars.get("logger");
34: * logger.debug("hello logger!");
35: * </pre>
36: *
37: * @author Zoltan Luspai
38: */
39: public class LogRegister implements Register {
40: //~ Methods ////////////////////////////////////////////////////////////////
41:
42: /**
43: * @see com.opensymphony.workflow.Register#registerVariable(com.opensymphony.workflow.WorkflowContext,com.opensymphony.workflow.spi.WorkflowEntry,java.util.Map,PropertySet)
44: */
45: public Object registerVariable(WorkflowContext context,
46: WorkflowEntry entry, Map args, PropertySet ps) {
47: String workflowname = "unknown";
48: long workflow_id = -1;
49:
50: if (entry != null) {
51: workflowname = entry.getWorkflowName();
52: workflow_id = entry.getId();
53: }
54:
55: boolean groupByInstance = false;
56: String useInstance = (String) args.get("addInstanceId");
57:
58: if (useInstance != null) {
59: groupByInstance = TextUtils.parseBoolean(useInstance);
60: }
61:
62: String categoryName = "OSWorkflow";
63:
64: if (args.get("Category") != null) {
65: categoryName = (String) args.get("Category");
66: }
67:
68: String category = categoryName + "." + workflowname;
69:
70: if (groupByInstance) {
71: category += ("." + (Long.toString(workflow_id)));
72: }
73:
74: Log log = LogFactory.getLog(category);
75:
76: return log;
77: }
78: }
|