home / skills / levnikolaevich / claude-code-skills / ln-771-logging-configurator

ln-771-logging-configurator skill

/ln-771-logging-configurator

This skill configures structured JSON logging for .NET and Python projects, generating environment-aware settings and enrichment for observability.

npx playbooks add skill levnikolaevich/claude-code-skills --skill ln-771-logging-configurator

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
6.4 KB
---
name: ln-771-logging-configurator
description: Configures structured logging (Serilog/.NET, structlog/Python)
---

# ln-771-logging-configurator

**Type:** L3 Worker
**Category:** 7XX Project Bootstrap
**Parent:** ln-770-crosscutting-setup

Configures structured JSON logging for .NET and Python projects.

---

## Overview

| Aspect | Details |
|--------|---------|
| **Input** | Context Store from ln-770 |
| **Output** | Logging configuration files |
| **Stacks** | .NET (Serilog), Python (structlog) |

---

## Phase 1: Receive Context

Accept Context Store from coordinator.

**Required Context:**
- `STACK`: .NET or Python
- `FRAMEWORK`: ASP.NET Core or FastAPI
- `FRAMEWORK_VERSION`: Version number
- `PROJECT_ROOT`: Project directory path
- `ENVIRONMENT`: Development or Production

**Validation:**
- If `STACK` not provided, detect from project files
- If version not provided, use latest stable

---

## Phase 2: Research Current Best Practices

Use MCP tools to get up-to-date documentation.

**For .NET (Serilog):**
```
MCP ref: "Serilog ASP.NET Core structured logging configuration"
Context7: /serilog/serilog-aspnetcore
```

**For Python (structlog):**
```
MCP ref: "structlog Python structured logging configuration"
Context7: /hynek/structlog
```

**Key Patterns to Research:**
1. Request logging middleware
2. Log enrichment (correlation ID, user context)
3. Log level configuration by environment
4. Sink configuration (Console, File, Seq, Elastic)

---

## Phase 3: Decision Points

Ask user for configuration preferences.

### Q1: Log Format

| Option | When to Use |
|--------|-------------|
| **JSON** (Recommended for Production) | Machine-readable, log aggregation systems |
| **Pretty/Colored** (Recommended for Development) | Human-readable, local debugging |

### Q2: Enrichment Fields

| Field | Description | Default |
|-------|-------------|---------|
| `correlationId` | Request tracking across services | ✓ Yes |
| `userId` | Authenticated user identifier | ✓ Yes |
| `requestPath` | HTTP request path | ✓ Yes |
| `responseTime` | Request duration in ms | ✓ Yes |
| `machineName` | Server hostname | Optional |
| `threadId` | Thread identifier | Optional |

### Q3: Log Sinks

| Sink | Use Case |
|------|----------|
| **Console** | Always enabled |
| **File** | Local persistence, log rotation |
| **Seq** | Structured log server |
| **Elasticsearch** | Log aggregation at scale |

### Q4: Log Levels by Environment

| Level | Development | Production |
|-------|-------------|------------|
| Default | Debug | Information |
| Microsoft.* | Information | Warning |
| System.* | Information | Warning |
| Application | Debug | Information |

---

## Phase 4: Generate Configuration

Generate files based on stack and decisions.

### .NET Output Files

| File | Purpose |
|------|---------|
| `Extensions/LoggingExtensions.cs` | Service registration |
| `appsettings.json` (update) | Serilog configuration |
| `appsettings.Development.json` (update) | Dev overrides |

**Generation Process:**
1. Use MCP ref to get current Serilog API
2. Generate LoggingExtensions.cs with:
   - UseSerilog configuration
   - Request logging middleware
   - Enrichment configuration
3. Update appsettings.json with Serilog section

**Packages to Add:**
- `Serilog.AspNetCore`
- `Serilog.Sinks.Console`
- `Serilog.Sinks.File` (if File sink selected)
- `Serilog.Enrichers.Environment` (if machineName selected)

### Python Output Files

| File | Purpose |
|------|---------|
| `core/logging_config.py` | structlog configuration |
| `middleware/logging_middleware.py` | Request logging |

**Generation Process:**
1. Use MCP ref to get current structlog API
2. Generate logging_config.py with:
   - Processor chain configuration
   - Renderer selection (JSON/Console)
   - Log level configuration
3. Generate logging_middleware.py for FastAPI

**Packages to Add:**
- `structlog`
- `python-json-logger` (if JSON format)

---

## Phase 5: Validate

Verify the configuration works.

**Validation Steps:**

1. **Check imports:** Ensure all packages are available
   - .NET: `dotnet list package | grep Serilog`
   - Python: `pip list | grep structlog`

2. **Syntax check:**
   - .NET: `dotnet build --no-restore`
   - Python: `python -m py_compile core/logging_config.py`

3. **Test log output:**
   - Start application
   - Make test request
   - Verify log format matches configuration

**Expected Log Format:**
```json
{
  "timestamp": "2026-01-10T12:00:00.000Z",
  "level": "info",
  "message": "Request completed",
  "correlationId": "abc-123",
  "requestPath": "/api/health",
  "responseTime": 45,
  "statusCode": 200
}
```

---

## Return to Coordinator

Return result to ln-770:

```json
{
  "status": "success",
  "files_created": [
    "Extensions/LoggingExtensions.cs",
    "appsettings.json"
  ],
  "packages_added": [
    "Serilog.AspNetCore",
    "Serilog.Sinks.Console"
  ],
  "registration_code": "services.AddLoggingServices(configuration);",
  "message": "Configured structured logging with Serilog"
}
```

---

## Idempotency

This skill is idempotent:
- Phase 1: Check if logging already configured (Grep for Serilog/structlog)
- If configured: Return `{ "status": "skipped", "message": "Logging already configured" }`
- If not: Proceed with configuration

---

## Reference Links

- [Serilog.AspNetCore](https://github.com/serilog/serilog-aspnetcore)
- [structlog Documentation](https://www.structlog.org/)
- [ASP.NET Core Logging](https://learn.microsoft.com/aspnet/core/fundamentals/logging/)

---

## Critical Rules

- **Use MCP ref/Context7 for current API** — do not hardcode Serilog/structlog config from memory
- **Idempotent** — if Serilog or structlog already configured, return `status: "skipped"` immediately
- **Environment-aware log levels** — Debug for Development, Information for Production (never Warning default)
- **Always include correlation ID enrichment** — required for distributed tracing
- **Return structured response** — `files_created`, `packages_added`, `registration_code` for coordinator aggregation

## Definition of Done

- Context Store received and validated (stack, framework, version)
- Best practices researched via MCP tools for target stack
- User decisions collected (format, enrichment, sinks, log levels)
- Configuration files generated (extensions/config + appsettings or Python modules)
- Syntax validated (`dotnet build` or `py_compile`)
- Structured JSON response returned to ln-770 coordinator

---

**Version:** 2.0.0
**Last Updated:** 2026-01-10

Overview

This skill configures structured JSON logging for .NET (Serilog) and Python (structlog) projects. It accepts a project context, validates stack and framework, and generates logging config files, middleware, and environment-aware settings. The skill is idempotent and returns a structured result listing created files, packages added, and registration snippets.

How this skill works

The skill reads the Context Store (STACK, FRAMEWORK, PROJECT_ROOT, ENVIRONMENT, etc.), detects missing values, and fetches current best practices via MCP references. It prompts for choices (format, enrichments, sinks, log levels), generates the appropriate files (LoggingExtensions.cs + appsettings.* for .NET or logging_config.py + middleware for Python), and updates package lists. Finally it validates imports, performs a syntax/build check, runs a test request, and returns a structured status payload.

When to use it

  • Bootstrapping logging for a new .NET or FastAPI service
  • Adding structured JSON logging to an existing project that lacks Serilog/structlog
  • Standardizing logs across services for aggregation and observability
  • Ensuring environment-aware log levels and required enrichments are present

Best practices

  • Prefer JSON output for production and pretty/colored console for development
  • Always enrich logs with correlationId and user context for traceability
  • Configure sinks by purpose: Console always, File for local persistence, Seq/Elasticsearch for aggregation
  • Make log levels environment-aware: Debug in Development, Information in Production
  • Keep the configuration idempotent: detect existing Serilog/structlog and skip if present

Example use cases

  • Generate Serilog setup and appsettings updates for an ASP.NET Core microservice
  • Add structlog processor chain and FastAPI middleware to a Python service
  • Switch production logs to JSON and route to Elasticsearch or Seq
  • Validate logging setup with automated build and a test HTTP request

FAQ

What inputs are required?

Provide STACK, FRAMEWORK, PROJECT_ROOT, and ENVIRONMENT; the skill will attempt auto-detection if any are missing.

How does idempotency work?

The skill greps project files for Serilog or structlog; if found it returns status: "skipped" and does not modify files.