# Labeling Logs with Folder Name

**URL:** <https://community.grafana.com/t/labeling-logs-with-folder-name/89460>\
**Category:** Grafana Alloy\
**Tags:** windows, promtail\
**Created:** [June 12, 2023, 9:20pm UTC](https://community.grafana.com/t/labeling-logs-with-folder-name/89460 "2023-06-12T21:20:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremyvermillion](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@jeremyvermillion](https://community.grafana.com/u/jeremyvermillion)\
**Post date:** [June 12, 2023, 9:20pm UTC](https://community.grafana.com/t/labeling-logs-with-folder-name/89460/1 "2023-06-12T21:20:57Z")

</div>

I am trying to label all logs that come out of a folder with the name of the folder. This is my current config. Basically I want the logs to be labeled with the name of the \* value. I’ve tried different regex methods i’ve found but anything I try keeps Grafana Agent service from starting.

```auto
logs:
  positions_directory: C:\ProgramData\grafana-agent\log-positions
  configs:
    - name: default
      positions:
        filename: /tmp/positions.yaml
      scrape_configs:
        - job_name: BackgroundIndexer
          static_configs:
            - targets:
              - localhost
              labels:
                job: BackgroundIndexer
                __path__ : L:\BackgroundIndexer\*\*.txt

```

---

<div class="post-metadata">

**Author:** ![jeremyvermillion](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@jeremyvermillion](https://community.grafana.com/u/jeremyvermillion)\
**Post date:** [June 13, 2023, 6:23pm UTC](https://community.grafana.com/t/labeling-logs-with-folder-name/89460/2 "2023-06-13T18:23:47Z")

</div>

Resolved it with help from this link

> <https://github.com/grafana/loki/issues/775>
>
> In relabel\_configs, I would like to get the filename label value to rewrite to a… specific label.
> 
> For instance, there are some log files in the directory.
> \- /logs/example-out.log
> \- /logs/example-error.log
> 
> I expect set a label called "service" equal to "example"
> 
> Then, I have the setting below in the relabel\_configs of promtail
> 
> \`\`\`yaml
> static\_configs:
> \- targets:
> - localhost
> labels:
> job: test
> \_\_path\_\_: /logs/\*.log
> relabel\_configs:
> \- source\_labels: \[filename\]
> regex: '^.\*\\/(.+)-(out|error)\\.log$'
> replacement: '${1}'
> target\_label: service
> action: replace
> \`\`\`
> 
> I have tested "filename" and "\_\_path\_\_" into the source\_labels.
> 
> There is no value return when source\_labels set to "filename".  
> When the source\_label set to "\_\_path\_\_", the value equals to "/logs/\*.log" 
> 
> How can i get the correct file path in relabel\_configs?

```auto
logs:
  positions_directory: C:\ProgramData\grafana-agent\log-positions
  configs:
    - name: default
      positions:
        filename: /tmp/positions.yaml
      scrape_configs:
        - job_name: BackgroundIndexer
          static_configs:
            - targets:
              - localhost
              labels:
                job: BackgroundIndexer
                __path__ : L:\BackgroundIndexer\*\*
          pipeline_stages:
          - match:
              selector: '{job="BackgroundIndexer"}'
              stages:
              - regex:
                  source: filename
                  expression: 'L:\\BackgroundIndexer\\(?P<customer>[^\\]+)\\'
              - labels:
                  customer:

```
