# Logging inside Java Stream

### Background

Once upon a time, I wanted to log something that was executed inside the Java [stream](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html) for debugging purposes.

But, I didn't see anything logged there.

### Solution

Put the log inside the `peek()` method instead of `map()`.

### Example

Previously, I was logging in this way (**which didn't work**),

```java
Stream.of("one", "two", "three", "four")
         .filter(e -> e.length() > 3)
         .peek(e -> System.out.println("Filtered value: " + e))
         .map(String::toUpperCase)
         .peek(e -> System.out.println("Mapped value: " + e))
         .collect(Collectors.toList());

Stream.of("one", "two", "three", "four")
    .map(val -> {
        System.out.println("Value length: " + val.length();
        return val;
    })  
    .filter(val -> val.length() > 3)
    .collect(Collectors.toList());
```

To make **it works**, I moved the logging to `peek()` ,

```java
Stream.of("one", "two", "three", "four")
    .peek(val -> System.out.println("Value length: " + val.length()))    
    .filter(val -> val.length() > 3)
    .collect(Collectors.toList());
```
