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

$MaxMessageSize 64k
# ...
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

compose.yml

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

viewer UI

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.8-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: log2s3-0.8-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for log2s3-0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 63ca89347ee43c3bc887722cae2f8be097ed74928f0b215142845eaa9392b265
MD5 778c86cb415703471a87d6ab76bf7b1c
BLAKE2b-256 6413ea10b7740bb515fa8fcc5e2ef428de0a90c873ca21976ba8418ff948b3ff

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