001/*
002 * (C) Copyright 2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Sun Seng David TAN <stan@nuxeo.com>
018 */
019
020package org.nuxeo.ecm.automation.core.operations;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028
029/**
030 * An operation to log in log4j.
031 *
032 * @since 5.6
033 */
034@Operation(id = LogOperation.ID, category = Constants.CAT_NOTIFICATION, label = "Log", description = "Logging with log4j", aliases = { "LogOperation" })
035public class LogOperation {
036
037    public static final String ID = "Log";
038
039    @Param(name = "category", required = false)
040    protected String category;
041
042    @Param(name = "message", required = true)
043    protected String message;
044
045    @Param(name = "level", required = true, widget = Constants.W_OPTION, values = { "info", "debug", "warn", "error" })
046    protected String level = "info";
047
048    @OperationMethod
049    public void run() {
050        if (category == null) {
051            category = "org.nuxeo.ecm.automation.logger";
052        }
053
054        Log log = LogFactory.getLog(category);
055
056        if ("debug".equals(level)) {
057            log.debug(message);
058            return;
059        }
060
061        if ("warn".equals(level)) {
062            log.warn(message);
063            return;
064        }
065
066        if ("error".equals(level)) {
067            log.error(message);
068            return;
069        }
070        // in any other case, use info log level
071        log.info(message);
072
073    }
074}