You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by peter-gergely-horvath <gi...@git.apache.org> on 2017/05/03 10:10:07 UTC

[GitHub] nifi pull request #1737: NIFI-3763 Add new processor to log user defined mes...

GitHub user peter-gergely-horvath opened a pull request:

    https://github.com/apache/nifi/pull/1737

    NIFI-3763 Add new processor to log user defined messages built with N…

    This pull request adds support described in [NIFI-3763: Add new processor to log user defined messages built with NiFi Expression Language](https://issues.apache.org/jira/browse/NIFI-3763)
    
    Notes:
    - Contribution check Maven build (`mvn -Pcontrib-check clean install`) completed **SUCCESSFULLY** 
    - The request has been rebased aginst NiFi master
    - This pull request does **NOT** bring new dependencies
    - Properties added to the processor have their appropriate display name "_Log Level_", "_Log prefix_" and "_Log message_"
    - This change is accompanied by a unit test, see `org.apache.nifi.processors.standard.TestLogString `
    
    -- BEGIN OF ORIGINAL PULL MESSAGE TEMPLATE -- 
    
    Thank you for submitting a contribution to Apache NiFi.
    
    In order to streamline the review of the contribution we ask you
    to ensure the following steps have been taken:
    
    ### For all changes:
    - [ ] Is there a JIRA ticket associated with this PR? Is it referenced 
         in the commit message?
    
    - [ ] Does your PR title start with NIFI-XXXX where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
    
    - [ ] Has your PR been rebased against the latest commit within the target branch (typically master)?
    
    - [ ] Is your initial contribution a single, squashed commit?
    
    ### For code changes:
    - [ ] Have you ensured that the full suite of tests is executed via mvn -Pcontrib-check clean install at the root nifi folder?
    - [ ] Have you written or updated unit tests to verify your changes?
    - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? 
    - [ ] If applicable, have you updated the LICENSE file, including the main LICENSE file under nifi-assembly?
    - [ ] If applicable, have you updated the NOTICE file, including the main NOTICE file found under nifi-assembly?
    - [ ] If adding new Properties, have you added .displayName in addition to .name (programmatic access) for each of the new properties?
    
    ### For documentation related changes:
    - [ ] Have you ensured that format looks appropriate for the output in which it is rendered?
    
    ### Note:
    Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/peter-gergely-horvath/nifi master

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/nifi/pull/1737.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #1737
    
----

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #1737: NIFI-3763 Add new processor to log user defined messages b...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on the issue:

    https://github.com/apache/nifi/pull/1737
  
    +1 LGTM, ran the tests again and tried on a NiFi instance, using prefix and/or message with and without Expression Language.  Thanks again for the great addition! Merging to master


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1737: NIFI-3763 Add new processor to log user defined mes...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/nifi/pull/1737


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1737: NIFI-3763 Add new processor to log user defined mes...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1737#discussion_r116506100
  
    --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LogString.java ---
    @@ -0,0 +1,208 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.processors.standard;
    +
    +import org.apache.nifi.annotation.behavior.EventDriven;
    +import org.apache.nifi.annotation.behavior.InputRequirement;
    +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
    +import org.apache.nifi.annotation.behavior.SideEffectFree;
    +import org.apache.nifi.annotation.behavior.SupportsBatching;
    +import org.apache.nifi.annotation.documentation.CapabilityDescription;
    +import org.apache.nifi.annotation.documentation.Tags;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.flowfile.FlowFile;
    +import org.apache.nifi.logging.ComponentLog;
    +import org.apache.nifi.processor.AbstractProcessor;
    +import org.apache.nifi.processor.ProcessContext;
    +import org.apache.nifi.processor.ProcessSession;
    +import org.apache.nifi.processor.ProcessorInitializationContext;
    +import org.apache.nifi.processor.Relationship;
    +import org.apache.nifi.processor.exception.ProcessException;
    +import org.apache.nifi.processor.util.StandardValidators;
    +import org.eclipse.jetty.util.StringUtil;
    +
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.HashSet;
    +import java.util.List;
    +import java.util.Set;
    +
    +@EventDriven
    +@SideEffectFree
    +@SupportsBatching
    +@Tags({"attributes", "logging"})
    +@InputRequirement(Requirement.INPUT_REQUIRED)
    +@CapabilityDescription("Emits a log message at the specified log level")
    +public class LogString extends AbstractProcessor {
    --- End diff --
    
    Although "String" is a ubiquitous term, most of the documentation (description, properties, e.g.) refers to logging a "message". Although not required, I recommend changing the name of this processor to LogMessage, what do you think?  If you agree, be sure to update the resources file and test class name too, please and thanks!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi pull request #1737: NIFI-3763 Add new processor to log user defined mes...

Posted by mattyb149 <gi...@git.apache.org>.
Github user mattyb149 commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1737#discussion_r116506707
  
    --- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LogString.java ---
    @@ -0,0 +1,208 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +package org.apache.nifi.processors.standard;
    +
    +import org.apache.nifi.annotation.behavior.EventDriven;
    +import org.apache.nifi.annotation.behavior.InputRequirement;
    +import org.apache.nifi.annotation.behavior.InputRequirement.Requirement;
    +import org.apache.nifi.annotation.behavior.SideEffectFree;
    +import org.apache.nifi.annotation.behavior.SupportsBatching;
    +import org.apache.nifi.annotation.documentation.CapabilityDescription;
    +import org.apache.nifi.annotation.documentation.Tags;
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.flowfile.FlowFile;
    +import org.apache.nifi.logging.ComponentLog;
    +import org.apache.nifi.processor.AbstractProcessor;
    +import org.apache.nifi.processor.ProcessContext;
    +import org.apache.nifi.processor.ProcessSession;
    +import org.apache.nifi.processor.ProcessorInitializationContext;
    +import org.apache.nifi.processor.Relationship;
    +import org.apache.nifi.processor.exception.ProcessException;
    +import org.apache.nifi.processor.util.StandardValidators;
    +import org.eclipse.jetty.util.StringUtil;
    +
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.HashSet;
    +import java.util.List;
    +import java.util.Set;
    +
    +@EventDriven
    +@SideEffectFree
    +@SupportsBatching
    +@Tags({"attributes", "logging"})
    +@InputRequirement(Requirement.INPUT_REQUIRED)
    +@CapabilityDescription("Emits a log message at the specified log level")
    +public class LogString extends AbstractProcessor {
    +
    +    public static final PropertyDescriptor LOG_LEVEL = new PropertyDescriptor.Builder()
    +            .name("Log Level")
    +            .required(true)
    +            .description("The Log Level to use when logging the message")
    +            .allowableValues(MessageLogLevel.values())
    +            .defaultValue(MessageLogLevel.info.toString())
    +            .expressionLanguageSupported(true)
    +            .build();
    +
    +    public static final PropertyDescriptor LOG_PREFIX = new PropertyDescriptor.Builder()
    +            .name("Log prefix")
    +            .required(false)
    +            .description("Log prefix appended to the log lines. " +
    +                    "It helps to distinguish the output of multiple LogString processors.")
    +            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    +            .expressionLanguageSupported(true)
    +            .build();
    +
    +    public static final PropertyDescriptor LOG_MESSAGE = new PropertyDescriptor.Builder()
    +            .name("Log message")
    +            .required(false)
    +            .description("The log message to emit")
    +            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    +            .expressionLanguageSupported(true)
    +            .build();
    +
    +    public static final Relationship REL_SUCCESS = new Relationship.Builder()
    +            .name("success")
    +            .description("All FlowFiles are routed to this relationship")
    +            .build();
    +
    +    private static final int CHUNK_SIZE = 50;
    +
    +    enum MessageLogLevel {
    +
    +        trace, debug, info, warn, error
    +    }
    +
    +    private Set<Relationship> relationships;
    +    private List<PropertyDescriptor> supportedDescriptors;
    +
    +    @Override
    +    protected void init(final ProcessorInitializationContext context) {
    +        final Set<Relationship> procRels = new HashSet<>();
    +        procRels.add(REL_SUCCESS);
    +        relationships = Collections.unmodifiableSet(procRels);
    +
    +        // descriptors
    +        final List<PropertyDescriptor> supDescriptors = new ArrayList<>();
    +        supDescriptors.add(LOG_LEVEL);
    +        supDescriptors.add(LOG_PREFIX);
    +        supDescriptors.add(LOG_MESSAGE);
    +        supportedDescriptors = Collections.unmodifiableList(supDescriptors);
    +    }
    +
    +    @Override
    +    public Set<Relationship> getRelationships() {
    +        return relationships;
    +    }
    +
    +    @Override
    +    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
    +        return supportedDescriptors;
    +    }
    +
    +    @Override
    +    public void onTrigger(final ProcessContext context, final ProcessSession session) {
    +
    +        final String logLevelValue = context.getProperty(LOG_LEVEL).getValue().toLowerCase();
    +
    +        final MessageLogLevel logLevel;
    +        try {
    +            logLevel = MessageLogLevel.valueOf(logLevelValue);
    +        } catch (Exception e) {
    +            throw new ProcessException(e);
    +        }
    +
    +        final ComponentLog logger = getLogger();
    +        boolean isLogLevelEnabled = false;
    +        switch (logLevel) {
    +            case trace:
    +                isLogLevelEnabled = logger.isTraceEnabled();
    +                break;
    +            case debug:
    +                isLogLevelEnabled = logger.isDebugEnabled();
    +                break;
    +            case info:
    +                isLogLevelEnabled = logger.isInfoEnabled();
    +                break;
    +            case warn:
    +                isLogLevelEnabled = logger.isWarnEnabled();
    +                break;
    +            case error:
    +                isLogLevelEnabled = logger.isErrorEnabled();
    +                break;
    +        }
    +
    +        if (!isLogLevelEnabled) {
    +            transferChunk(session);
    +            return;
    +        }
    +
    +        final FlowFile flowFile = session.get();
    +        if (flowFile == null) {
    +            return;
    +        }
    +
    +        processFlowFile(logger, logLevel, flowFile, context);
    +        session.transfer(flowFile, REL_SUCCESS);
    +    }
    +
    +    private void processFlowFile(
    +            final ComponentLog logger,
    +            final MessageLogLevel logLevel,
    +            final FlowFile flowFile,
    +            final ProcessContext context) {
    +
    +        String logPrefix = context.getProperty(LOG_PREFIX).evaluateAttributeExpressions(flowFile).getValue();
    +        String logMessage = context.getProperty(LOG_MESSAGE).evaluateAttributeExpressions(flowFile).getValue();
    +
    +        String messageToWrite;
    +        if (StringUtil.isBlank(logPrefix)) {
    +            messageToWrite = logMessage;
    +        } else {
    +            messageToWrite = String.format("%s %s", logPrefix, logMessage);
    --- End diff --
    
    Do you think the extra space between the prefix and message could throw off the user at first? Certainly if they look in the logs they could see there is a space between what they have specified for the prefix vs the message, but I'm wondering if it is best to omit the space or update the property documentation to make it clear there will or will not be whitespace between the prefix and message. I mention it only because I left a space at the end of my prefix and tried to search for the prefix+message and didn't find it right away. Again, not a requirement, just a suggestion/question :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] nifi issue #1737: NIFI-3763 Add new processor to log user defined messages b...

Posted by peter-gergely-horvath <gi...@git.apache.org>.
Github user peter-gergely-horvath commented on the issue:

    https://github.com/apache/nifi/pull/1737
  
    I think all of them make sense, so I've updated the code according to your proposals. :)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---