Logstash中如何从上下文根指定路径而非使用完整路径
Hey there! Let's work through that path issue you're hitting with Logstash's file plugin.
First, let's clear up why using /../../ didn't work: Logstash resolves file paths in your configuration relative to the directory you were in when you started Logstash, not the directory where your config file is stored. So when you tried those parent directory traversals, you were probably jumping to a different root than you expected (like your user home folder instead of /home/username/logstash).
Here are the most reliable ways to reference paths relative to your Logstash root directory:
1. Launch Logstash from your root directory and use relative paths
The simplest fix is to switch to your Logstash root folder before starting the service, then use relative paths in your config.
For example:
- Navigate to your Logstash root directory:
cd /home/username/logstash - Start Logstash pointing to your config file (using a relative path from the root):
bin/logstash -f usage/confs/your-config-file.conf - Update the file plugin path in your config to be relative to the root:
path => "./logs/inputs/your-log-files*"
This ensures all paths in your config resolve correctly against /home/username/logstash.
2. Use environment variables for flexible root path references
If you don't want to depend on starting Logstash from a specific directory, use an environment variable to define your root path. This is perfect for keeping your config portable across different environments.
Here's how to set it up:
- Define the environment variable before starting Logstash:
export LOGSTASH_ROOT="/home/username/logstash" bin/logstash -f usage/confs/your-config-file.conf - Reference the variable directly in your Logstash config:
path => "${LOGSTASH_ROOT}/logs/inputs/your-log-files*"
Logstash will automatically substitute the environment variable value when parsing the config, so your path will always point to the correct root no matter where you start the service.
3. Bonus tip: Avoid hardcoding absolute paths
Sticking to either of the above methods eliminates hardcoded absolute paths in your config, making your setup easier to maintain. If you ever move your Logstash installation, you just need to update the working directory or environment variable instead of editing every config file.
内容的提问来源于stack exchange,提问作者Orkun

