001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Sun Seng David TAN <stan@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.automation.core.operations;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.ecm.automation.core.Constants;
023import org.nuxeo.ecm.automation.core.annotations.Operation;
024import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
025import org.nuxeo.ecm.automation.core.annotations.Param;
026
027/**
028 * An operation to log in log4j.
029 *
030 * @since 5.6
031 */
032@Operation(id = LogOperation.ID, category = Constants.CAT_NOTIFICATION, label = "Log", description = "Logging with log4j", aliases = { "LogOperation" })
033public class LogOperation {
034
035    public static final String ID = "Log";
036
037    @Param(name = "category", required = false)
038    protected String category;
039
040    @Param(name = "message", required = true)
041    protected String message;
042
043    @Param(name = "level", required = true, widget = Constants.W_OPTION, values = { "info", "debug", "warn", "error" })
044    protected String level = "info";
045
046    @OperationMethod
047    public void run() {
048        if (category == null) {
049            category = "org.nuxeo.ecm.automation.logger";
050        }
051
052        Log log = LogFactory.getLog(category);
053
054        if ("debug".equals(level)) {
055            log.debug(message);
056            return;
057        }
058
059        if ("warn".equals(level)) {
060            log.warn(message);
061            return;
062        }
063
064        if ("error".equals(level)) {
065            log.error(message);
066            return;
067        }
068        // in any other case, use info log level
069        log.info(message);
070
071    }
072}