Skip to main content

log directory management CLI utility

Project description

log files managemant CLI utilitiy

prepare rsyslog/docker logging

/etc/docker/daemon.json

{
  "log-driver": "syslog",
  "log-opts": {
    "syslog-address": "tcp://localhost",
    "syslog-facility": "daemon",
    "syslog-format": "rfc5424micro",
    "tag": "container/{{.Name}}/{{.ID}}"
  }
}

/etc/rsyslog.conf

# ...
module(load="imtcp")
input(type="imtcp" port="514")
# ...
$ActionFileDefaultTemplate RSYSLOG_FileFormat

$IncludeConfig /etc/rsyslog.d/*.conf

/etc/rsyslog.d/10-container.conf

template(name="spcontainerfile" type="string" string="/var/log/container/%syslogtag:R,ERE,1,FIELD:container/([a-z0-9]*).*--end%/%$YEAR%-%$MONTH%-%$DAY%.log")
template(name="containerfile" type="string" string="/var/log/container/default/%$YEAR%-%$MONTH%-%$DAY%.log")
template(name="cmdcontainerfile" type="string" string="/var/log/container/run/%$YEAR%-%$MONTH%-%$DAY%.log")

# docker compose run
:syslogtag,contains,"-run-" ?cmdcontainerfile
& stop

# special containers
:syslogtag,startswith,"container/traefik/" ?spcontainerfile
& stop
# :syslogtag,startswith,"container/YOUR_CONTAINER_NAME/" ?spcontainerfile
# & stop

# other container logs
:syslogtag,startswith,"container/" ?containerfile
& stop

install (pip)

  • pip install log2s3
    • (optional) pip install zstd lz4 Brotli pyliblzfse zopfli python-snappy python-lzo pyzpaq zlib-ng
  • log2s3 [options]

install (docker)

  • docker pull ghcr.io/wtnb75/log2s3
  • docker run -u $(id -u syslog):$(id -g syslog) -v /var/log/container:/var/log/container -w /w ghcr.io/wtnb75/log2s3 [options]

subcommands

filetree

  • compress old/large log files
    • log2s3 filetree-compress --top /var/log/container --older 2d --bigger 4k --compress gzip
  • decompress all log files
    • log2s3 filetree-compress --top /var/log/container --compress decompress
  • remove old log files
    • log2s3 filetree-delete --top /var/log/container --older 30d

s3

option/environment variables

option env name description
--s3-access-key AWS_ACCESS_KEY_ID AWS Access Key
--s3-secret-key AWS_SECRET_ACCESS_KEY AWS Secret Key
--s3-region AWS_DEFAULT_REGION AWS Region
--s3-endpoint AWS_ENDPOINT_URL_S3 AWS Endpoint URL for S3
--s3-bucket AWS_S3_BUCKET AWS S3 Bucket name
--dotenv load .env for S3 client config
--prefix object key prefix
  • make bucket
    • log2s3 s3-make-bucket --s3-bucket mytestbucket123
  • list buckets
    • log2s3 s3-bucket
  • list objects
    • log2s3 s3-list
  • du
    • log2s3 s3-du
  • compress and upload to S3 object storage
    • log2s3 s3-put-tree --top /var/log/container --prefix $(hostname -s)/ --older 7d --compress xz
  • remove by object key suffix
    • log2s3 s3-delete-by --prefix $(hostname -s)/ --suffix .gz

cat/view/edit

  • local files
    • log2s3 cat /path/to/file.gz
    • log2s3 less /path/to/file.xz
    • log2s3 vi /path/to/file.bz2
  • s3 objects
    • log2s3 s3-cat path/to/file.gz
    • log2s3 s3-less path/to/file.xz
    • log2s3 s3-vi path/to/file.bz2

others

  • compress benchmark
    • log2s3 compress-benchmark /path/to/file

examples

docker-compose.yml

version: '3'

services:
  logcompress:
    image: ghcr.io/wtnb75/log2s3
    container_name: logcompress
    volumes:
    - /var/log/container:/work
    user: "107:4"  # syslog:adm
    command:
    - filetree-compress
    - --top
    - /work
    - --older
    - 2d
    - --newer
    - 7d
    - --bigger
    - 4k
    - --compress
    - gzip
    profiles:
    - cli
  logstage:
    image: ghcr.io/wtnb75/log2s3
    container_name: logstage
    volumes:
    - /var/log/container:/work:ro
    command:
    - s3-put-tree
    - --top
    - /work
    - --older
    - 7d
    - --newer
    - 14d
    - --compress
    - xz
    - --prefix
    - container-logs/
    environment:
      AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY}
      AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_KEY}
      AWS_ENDPOINT_URL_S3: ${AWS_ENDPOINT_URL_S3}
      AWS_REGION: ${AWS_REGION}
      AWS_S3_BUCKET: ${AWS_S3_BUCKET}
  loggc:
    image: ghcr.io/wtnb75/log2s3
    container_name: loggc
    volumes:
    - /var/log/container:/work
    user: "107:4"  # syslog:adm
    command:
    - filetree-delete
    - --top
    - /work
    - --older
    - 400d

shell script

#! /bin/sh
set -eu

# compress old
log2s3 filetree-compress --top /var/log/container --older 2d --newer 7d --bigger 4k --compress gzip

# stage older
log2s3 s3-put-tree --top /var/log/container --older 7d --newer 14d --compress xz --dotenv --s3-bucket mytestbucket123 --prefix container-log/

# remove oldest
log2s3 filetree-delete --top /var/log/container --older 400d

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

log2s3-0.6-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file log2s3-0.6-py3-none-any.whl.

File metadata

  • Download URL: log2s3-0.6-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for log2s3-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2e8a0f0dc5edab76e6ffa37a55a9f039423219ce0629891e25e7d0cb593e6924
MD5 4a4e9c6991edcf7b2a054a0588af091a
BLAKE2b-256 063c7efb89c564772ad1f224c77b835cd3a6857e8f2ffc304186e81d71c42e5d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page