【问题标题】:I need to automate write-log for this function我需要为此功能自动写入日志
【发布时间】:2020-07-09 15:25:42
【问题描述】:

我有以下函数write-log,我想把运行脚本时生成的日志放在一个子文件夹中,并将它们重命名为file.logs.old。此外,我想将最后的日志保留在脚本文件夹中。示例

最后的日志:

C:\scripts\powershellscript\logs.log

子文件夹旧日志:

C:\scripts\powershellscript\logfolder\

旧日志:

C:\scripts\powershellscript\logfolder\logs1.log.old
C:\scripts\powershellscript\logfolder\logs2.log.old
C:\scripts\powershellscript\logfolder\logs3.log.old
Etc...

我有以下功能,但它只保存最后一个日志并覆盖它:

function write-Log
{
    Param (
        [Parameter(Mandatory = $false)]
        #Message
        $Message,

        [Parameter(Mandatory = $false)]
        #Errormessage
        $ErrorMessage,

        [Parameter(Mandatory = $false)]
        #Component that create the error
        $Component,

        [Parameter(Mandatory = $false)]
        #Error Type
        [int]$Type,

        [Parameter(Mandatory = $true)]
        #Filepath to the Logfile
        $LogFile
    )

    #Create a Timestamp
    $Date = Get-Date -Format "MM/dd/yyyy"
    $time = Get-Date -Format "HH:mm:ss.fff"

    #If you type a error message the type will automatically switch to type 3 Error
    if ($null -ne $ErrorMessage)
    { $Type = 3
    }
    #If you type no Component the Component will set to space
    if ($null -eq $Component)
    { $Component = " "
    }
    #If you set the type variable it is set to 1 automatically
    if ($null -eq $Type)
    { $Type = 1
    }

    #create the log entry
    $LogMessage = "<![LOG[$Message  $ErrorMessage ]LOG]!>" + "<time=`"$time`" date=`"$Date`" component=`"$Component`" context=`"`" type=`"$Type`" thread=`"`" file=`"`">"
    #write the log entry
    $LogMessage | Out-File -Append -Encoding UTF8 -FilePath $LogFile

}

这是存储日志的当前变量

#Variable
$Logfile = "C:\Scripts\powershellscript\logs.log" #logfile path

我怎样才能做到这一点?我正在寻找需要添加到用户查询中的此功能的帮助。该脚本正在运行,但仅附加最后一个日志。并且长期检查旧日志是无法管理的。

谢谢

【问题讨论】:

  • [1] 编写和测试代码来做你想做的事。 [2] 将其转换为函数。 [3] 将该函数添加到脚本的开头。 [4] 在 Write-Log 函数中的 OR 之前调用它来处理该过程。
  • 嗨@Lee_Dailey,谢谢你的回答。我已经有了一个工作脚本,但它只保留和保存一个日志文件。我正在寻找的是在函数中自动创建日志,以便我将最后一个日志文件保存在脚本文件夹中,并将旧日志文件移动到子文件夹中进行归档。有什么方法可以进一步扩展我在帖子中为此范围添加的功能?据了解,您提到的 1、2、3、4 的所有步骤都有效,但只保存一个日志文件。
  • i 指的是要添加到您提供的代码中的 additional 函数。 [grin] 我会 [A] 在写入之前检查现有文件的大小或日期。 [B] 如果文件太大/太旧,则使用时间戳重命名文件。 ///// 另一种解决方案是每天创建一个新文件。像 - '是今天的文件吗?用它!如果不是从今天开始,请用今天的日期制作新的`-似乎涵盖了这个想法......而且它既简单又相当强大。
  • @Lee_Dailey,感谢您对此的意见。我发布了一些对我来说现在很好用的东西。问候,StackLad
  • 我也是这么想的!很高兴看到你让它根据需要为你工作! [咧嘴]

标签: function powershell logging parameters


【解决方案1】:

我想你可能会发现这个模块有用 - https://github.com/PoShLog/PoShLog

Install-Module PoShLog安装它

然后你可以像这样设置新的文件记录器:

Import-Module PoShLog

# Setup new logger
New-Logger |
    Set-MinimumLevel -Value Verbose |
    Add-SinkFile -Path 'C:\Scripts\Logs\Script-.log' -RollingInterval Minute  | # Add logging into file
    Start-Logger

# Try some logging methods
Write-InfoLog 'Some information to be logged...'

try {
    ConvertFrom-Json 'xxx'
}
catch {
    Write-ErrorLog 'Error occured!' -ErrorRecord $_
}

Close-Logger

这将每分钟创建新的日志文件,但您可以使用参数RollingInterval 更改间隔,您还可以限制日志目录中的文件数或文件大小,更多信息here in wiki。在上面的例子中我只展示了两个日志方法,但是有6 in total

我希望这对您有所帮助,如果您有任何问题,请查看PoShLog wiki 或通过discord 与我联系。编码愉快!

【讨论】:

  • 这是一个很好的选择,虽然它不是很简单并且需要开发者/社区的支持才能让它滚动。
【解决方案2】:

您要移动哪些日志?是你刚刚创建的还是之前创建的?

要移动文件,您可以使用mv 命令。

mv log old_log_folder/old_log

也许我没有理解您的要求。
最好的,

【讨论】:

  • 我正在构建一个脚本,以备将来使用,我想将“旧日志文件”移动到此文件夹,并仅保留脚本文件夹中的最后一个。这需要在函数中添加,以便在脚本期间完成该过程。我已经更新了帖子。我不想手动执行此操作,而是在函数中构建它。
【解决方案3】:

那个解决方案对我有用,现在我保留这个:

#Variable
#Add date to the log file name and date
$day = Get-Date -Format dd
$month = Get-Date -Format MM
$year = Get-Date -Format yyyy
$hour = Get-Date -Format HH
$minute = Get-Date -Format mm
$hoy = "$day" + "$month" + "$year" + "$hour" + "$minute"
New-Item C:\Scripts\Logs\Script-$hoy.log -type file -force
#Add end date

这样,通过时间戳和强制新项目,我总是在手动创建的日志文件夹中有一个新的日志文件。

尽管如此,我们仍然赞赏更好的解决方案。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多