-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add custom logging documentation to advanced tutorial #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+174
−6
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
19c7672
Add custom logging documentation for psake output handlers
claude a5cd444
Add psake-config.ps1 documentation
claude 2fe2203
Move psake-config doc to Advanced Techniques and clarify scope
claude 461fef2
Update logging-errors doc to reflect pluggable logging support
claude 2e67693
Add frontmatter to custom-logging and psake-config docs
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| --- | ||
| description: "How to override psake's output handlers for custom logging" | ||
| --- | ||
|
|
||
| # Custom Logging | ||
|
|
||
| psake routes all internal messages through configurable output handlers. You can override these handlers in your [`psake-config.ps1`](./psake-config.md) file to integrate with your own logging system. | ||
|
|
||
| ## Default Handlers | ||
|
|
||
| psake ships with handlers for six message types: | ||
|
|
||
| | Type | Default Behavior | | ||
| |------|-----------------| | ||
| | `heading` | Cyan colored output | | ||
| | `default` | `Write-Output` | | ||
| | `debug` | `Write-Debug` | | ||
| | `warning` | Yellow colored output | | ||
| | `error` | Red colored output | | ||
| | `success` | Green colored output | | ||
|
|
||
| Unknown message types fall back to the `default` handler. | ||
|
|
||
| ## Override Specific Message Types | ||
|
|
||
| To customize how individual message types are handled, override entries in `$config.outputHandlers` in your `psake-config.ps1`: | ||
|
|
||
| ```powershell | ||
| # psake-config.ps1 | ||
|
|
||
| # Send warnings to a log file instead of the console | ||
| $config.outputHandlers.warning = { | ||
| Param($output) | ||
| Add-Content -Path "build-warnings.log" -Value $output | ||
| } | ||
|
|
||
| # Suppress debug messages entirely | ||
| $config.outputHandlers.debug = { | ||
| Param($output) | ||
| # do nothing | ||
| } | ||
| ``` | ||
|
|
||
| Each handler receives a single `$output` parameter containing the message string. | ||
|
|
||
| ## Override All Logging | ||
|
|
||
| To replace the entire routing logic, override `$config.outputHandler` (singular). This script block receives both the message and its type, giving you full control: | ||
|
|
||
| ```powershell | ||
| # psake-config.ps1 | ||
|
|
||
| $config.outputHandler = { | ||
| Param($output, $type) | ||
| # Route everything through your custom logger | ||
| Write-MyBuildLog -Message $output -Level $type | ||
| } | ||
| ``` | ||
|
|
||
| When you override `outputHandler`, the individual `outputHandlers` entries are bypassed entirely. | ||
|
|
||
| ## Example: Log to a File | ||
|
|
||
| ```powershell | ||
| # psake-config.ps1 | ||
|
|
||
| $config.outputHandler = { | ||
| Param($output, $type) | ||
| $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | ||
| $line = "[$timestamp] [$type] $output" | ||
| Add-Content -Path "build.log" -Value $line | ||
| # Still write to console | ||
| Write-Host $line | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| --- | ||
| description: "How to customize psake defaults with psake-config.ps1" | ||
| --- | ||
|
|
||
| # psake Configuration File | ||
HeyItsGilbert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| psake loads a `psake-config.ps1` file at the start of every build to set default values for your build environment. You can use this file to change psake's default build file name, framework version, task name format, output handlers, and more. | ||
|
|
||
| :::note | ||
| Most projects do not need a `psake-config.ps1` file. psake's built-in defaults work well for the majority of use cases. Only create one if you need to change a specific default. | ||
| ::: | ||
|
|
||
| ## How psake Finds the Config File | ||
|
|
||
| psake searches for `psake-config.ps1` in two locations, in order: | ||
|
|
||
| 1. **The build script's directory** — the folder containing the build file passed to `Invoke-psake` | ||
| 2. **The psake module directory** — the folder where `psake.psm1` is installed | ||
|
|
||
| The first file found wins. If neither location contains a config file, psake uses its built-in defaults. | ||
|
|
||
| This means you can place a `psake-config.ps1` next to your `psakefile.ps1` to customize settings per-project, or place one alongside the psake module for machine-wide defaults. | ||
|
|
||
| ## Partial Overrides | ||
|
|
||
| Your config file does not need to set every property. psake initializes all properties to their defaults before loading your config file, so any property you omit keeps its default value. You only need to set the properties you want to change. | ||
|
|
||
| ## Configuration Properties | ||
|
|
||
| Inside `psake-config.ps1`, you set properties on the `$config` variable. Here is every available property: | ||
|
|
||
| | Property | Type | Default | Description | | ||
| |----------|------|---------|-------------| | ||
| | `buildFileName` | `string` | `"psakefile.ps1"` | Default build file name when none is specified | | ||
| | `legacyBuildFileName` | `string` | `"default.ps1"` | Fallback build file name (legacy support) | | ||
| | `framework` | `string` | `"4.0"` | .NET Framework version to target | | ||
| | `taskNameFormat` | `string` or `scriptblock` | `"Executing {0}"` | Format string or scriptblock for task name display | | ||
| | `verboseError` | `bool` | `$false` | Show detailed error information | | ||
| | `coloredOutput` | `bool` | `$true` | Enable colored console output | | ||
| | `modules` | `string[]` | `$null` | Module paths to auto-load before build execution | | ||
| | `moduleScope` | `string` | — | Scope for loaded modules | | ||
| | `outputHandler` | `scriptblock` | *(routes to outputHandlers)* | Master handler that receives all output | | ||
| | `outputHandlers` | `hashtable` | *(see below)* | Per-type output handlers | | ||
|
|
||
| ## Minimal Example | ||
|
|
||
| ```powershell title="psake-config.ps1" | ||
| # Use a different default build file name | ||
| $config.buildFileName = "build.ps1" | ||
|
|
||
| # Target .NET 4.8 | ||
| $config.framework = "4.8" | ||
|
|
||
| # Show detailed errors | ||
| $config.verboseError = $true | ||
| ``` | ||
|
|
||
| ## Task Name Formatting | ||
|
|
||
| `taskNameFormat` accepts either a format string or a scriptblock: | ||
|
|
||
| ```powershell title="psake-config.ps1" | ||
| # Format string — {0} is replaced with the task name | ||
| $config.taskNameFormat = "Executing {0}" | ||
|
|
||
| # Scriptblock — receives the task name as a parameter | ||
| $config.taskNameFormat = { | ||
| param($taskName) | ||
| "Executing $taskName at $(Get-Date)" | ||
| } | ||
| ``` | ||
|
|
||
| ## Auto-Loading Modules | ||
|
|
||
| Use `$config.modules` to load PowerShell modules before any tasks run: | ||
|
|
||
| ```powershell title="psake-config.ps1" | ||
| # Load all modules from a folder | ||
| $config.modules = @(".\modules\*.psm1") | ||
|
|
||
| # Load specific modules | ||
| $config.modules = @(".\modules\*.psm1", ".\my_module.psm1") | ||
| ``` | ||
|
|
||
| ## Output Handlers | ||
|
|
||
| psake routes all internal messages through configurable output handlers. For a full guide on customizing logging, see [Custom Logging](./custom-logging.md). | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Custom Logging](./custom-logging.md) — Override psake's output handlers | ||
| - [Configuration Reference](../reference/configuration-reference) — Full reference for `Invoke-psake` parameters and build script settings | ||
| - [Structure of a psake Build Script](./structure-of-a-psake-build-script.md) — How build scripts are organized | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.