PowerShell module for pushing log entries to Loki.
✔️ See CHANGELOG.md for what's new!
Using the latest version of PowerShellGet:
Install-Module -Name PSLoki -Repository PSGallery -Scope CurrentUser -Force -PassThru
Import-Module -Name PSLoki -Force -PassThru
Or if you already have the module installed, update to the latest version:
Update-Module -Name PSLoki
Import-Module -Name PSLoki -Force -PassThru
For use with e.g. Grafana Cloud you must have an account to access the API. Access tokens (API Keys) for Grafana can be generated at https://grafana.com/orgs/[your-user-name]/api-keys.
To authenticate, pass the generated access token using the -AccessToken
parameter with each call or set the LOKI_ACCESS_TOKEN
environment variable:
$env:LOKI_ACCESS_TOKEN = "<your access token>"
Use Get-Command -Module PSLoki
for a list of functions provided by this module. See the help associated with each function using the Get-Help
command, e.g. Get-Help Get-LokiLogEntry -Detailed
, and the documentation available in docs
for more details:
$timestamp = Get-LokiTimestamp
Write-Host "Current Unix Epoch (in nanoseconds) is: $timestamp"
$labels = @{
'label' = 'value'
'foo' = 'bar'
}
$logEntries = @(
@{
time = "1666644815000000000"
line = "log something"
}
@{
time = "1666644823000000000"
line = "log something else"
}
)
$response = Send-LokiLogEntry -URI "https://logs-prod-us-central1.grafana.net/loki/api/v1/push" -Labels $labels -Entries $logEntries
Write-Host "Log entries sent to Loki [$($response.StatusCode) $($response.StatusDescription)]"
The endpoint URL can also be specified by setting the LOKI_ENDPOINT
or LOKI_HOST
environment variables.
To view the actual Loki log entries sent in the requests, add the -Debug
switch to the command.
Example:
PS> Send-LokiLogEntry -Labels $labels -Entries $logEntries -Debug
DEBUG: Invoking web request: POST https://logs-prod-us-central1.grafana.net/loki/api/v1/push
DEBUG: Loki log entries: {
"streams": [
{
"values": [
[
"1666644815000000000",
"log something"
],
[
"1666644823000000000",
"log something else"
]
],
"stream": {
"label": "value",
"foo": "bar"
}
}
]
}