Skip to content

Commit e9b1b53

Browse files
authored
Merge pull request #7 from TheAngryByrd/getLoggerByQuotation
Add getLoggerByQuotation, getLoggerByFunc and deprecate getCurrentLogger
2 parents daa67ad + 0da4e69 commit e9b1b53

File tree

4 files changed

+96
-17
lines changed

4 files changed

+96
-17
lines changed

.paket/Paket.Restore.targets

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,43 @@
1717
<PaketExeImage Condition=" '$(PaketBootstrapperStyle)' == 'proj' ">native</PaketExeImage>
1818
<MonoPath Condition="'$(MonoPath)' == '' And Exists('/Library/Frameworks/Mono.framework/Commands/mono')">/Library/Frameworks/Mono.framework/Commands/mono</MonoPath>
1919
<MonoPath Condition="'$(MonoPath)' == ''">mono</MonoPath>
20-
<!-- Paket command -->
21-
<PaketExePath Condition=" '$(PaketExePath)' == '' AND Exists('$(PaketToolsPath)paket')">$(PaketToolsPath)paket</PaketExePath>
22-
<PaketExePath Condition=" '$(PaketExePath)' == '' AND Exists('$(PaketRootPath)paket.exe')">$(PaketRootPath)paket.exe</PaketExePath>
2320

24-
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' == 'Windows_NT' AND Exists('$(PaketToolsPath)paket.exe')">$(PaketToolsPath)paket.exe</PaketExePath>
25-
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' != 'Windows_NT' AND '$(PaketExeImage)' == 'assembly' AND Exists('$(PaketToolsPath)paket.exe') ">$(PaketToolsPath)paket.exe</PaketExePath>
26-
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' != 'Windows_NT' AND '$(PaketExeImage)' == 'native' AND Exists('$(PaketToolsPath)paket') ">$(PaketToolsPath)paket</PaketExePath>
27-
<PaketExePath Condition=" '$(PaketExePath)' == '' ">paket</PaketExePath>
21+
<!-- PaketBootStrapper -->
22+
<PaketBootStrapperExePath Condition=" '$(PaketBootStrapperExePath)' == '' AND Exists('$(PaketRootPath)paket.bootstrapper.exe')">$(PaketRootPath)paket.bootstrapper.exe</PaketBootStrapperExePath>
23+
<PaketBootStrapperExePath Condition=" '$(PaketBootStrapperExePath)' == '' ">$(PaketToolsPath)paket.bootstrapper.exe</PaketBootStrapperExePath>
24+
<PaketBootStrapperExeDir Condition=" Exists('$(PaketBootStrapperExePath)') " >$([System.IO.Path]::GetDirectoryName("$(PaketBootStrapperExePath)"))\</PaketBootStrapperExeDir>
25+
26+
<!-- Paket -->
27+
28+
<!-- windows, root => tool => proj style => bootstrapper => global -->
29+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' == 'Windows_NT' AND Exists('$(PaketRootPath)paket.exe') ">$(PaketRootPath)paket.exe</PaketExePath>
30+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' == 'Windows_NT' AND Exists('$(PaketToolsPath)paket.exe') ">$(PaketToolsPath)paket.exe</PaketExePath>
31+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' == 'Windows_NT' AND '$(PaketBootstrapperStyle)' == 'proj' ">$(PaketToolsPath)paket.exe</PaketExePath>
32+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' == 'Windows_NT' AND Exists('$(PaketBootStrapperExeDir)') ">$(_PaketBootStrapperExeDir)paket.exe</PaketExePath>
33+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' == 'Windows_NT' ">paket.exe</PaketExePath>
34+
35+
<!-- no windows, try native paket as default, root => tool => proj style => mono paket => bootstrpper => global -->
36+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' != 'Windows_NT' AND Exists('$(PaketRootPath)paket') ">$(PaketRootPath)paket</PaketExePath>
37+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' != 'Windows_NT' AND Exists('$(PaketToolsPath)paket') ">$(PaketToolsPath)paket</PaketExePath>
38+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' != 'Windows_NT' AND '$(PaketBootstrapperStyle)' == 'proj' ">$(PaketToolsPath)paket</PaketExePath>
39+
40+
<!-- no windows, try mono paket -->
41+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' != 'Windows_NT' AND Exists('$(PaketRootPath)paket.exe') ">$(PaketRootPath)paket.exe</PaketExePath>
42+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' != 'Windows_NT' AND Exists('$(PaketToolsPath)paket.exe') ">$(PaketToolsPath)paket.exe</PaketExePath>
43+
44+
<!-- no windows, try bootstrapper -->
45+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' != 'Windows_NT' AND Exists('$(PaketBootStrapperExeDir)') ">$(PaketBootStrapperExeDir)paket.exe</PaketExePath>
46+
47+
<!-- no windows, try global native paket -->
48+
<PaketExePath Condition=" '$(PaketExePath)' == '' AND '$(OS)' != 'Windows_NT' ">paket</PaketExePath>
2849

2950
<!-- Paket command -->
3051
<_PaketExeExtension>$([System.IO.Path]::GetExtension("$(PaketExePath)"))</_PaketExeExtension>
3152
<PaketCommand Condition=" '$(PaketCommand)' == '' AND '$(_PaketExeExtension)' == '.dll' ">dotnet "$(PaketExePath)"</PaketCommand>
3253
<PaketCommand Condition=" '$(PaketCommand)' == '' AND '$(OS)' != 'Windows_NT' AND '$(_PaketExeExtension)' == '.exe' ">$(MonoPath) --runtime=v4.0.30319 "$(PaketExePath)"</PaketCommand>
3354
<PaketCommand Condition=" '$(PaketCommand)' == '' ">"$(PaketExePath)"</PaketCommand>
3455

35-
<PaketBootStrapperExePath Condition=" '$(PaketBootStrapperExePath)' == '' AND Exists('$(PaketRootPath)paket.bootstrapper.exe')">$(PaketRootPath)paket.bootstrapper.exe</PaketBootStrapperExePath>
36-
<PaketBootStrapperExePath Condition=" '$(PaketBootStrapperExePath)' == '' ">$(PaketToolsPath)paket.bootstrapper.exe</PaketBootStrapperExePath>
56+
3757
<PaketBootStrapperCommand Condition=" '$(OS)' == 'Windows_NT'">"$(PaketBootStrapperExePath)"</PaketBootStrapperCommand>
3858
<PaketBootStrapperCommand Condition=" '$(OS)' != 'Windows_NT' ">$(MonoPath) --runtime=v4.0.30319 "$(PaketBootStrapperExePath)"</PaketBootStrapperCommand>
3959

@@ -42,6 +62,9 @@
4262
<!-- see https://github.com/fsharp/fslang-design/blob/master/RFCs/FS-1032-fsharp-in-dotnet-sdk.md -->
4363
<DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference>
4464
<DisableImplicitSystemValueTupleReference>true</DisableImplicitSystemValueTupleReference>
65+
66+
<!-- Disable Paket restore under NCrunch build -->
67+
<PaketRestoreDisabled Condition="'$(NCrunch)' == '1'">True</PaketRestoreDisabled>
4568
</PropertyGroup>
4669

4770
<Target Name="PaketBootstrapping" Condition="Exists('$(PaketToolsPath)paket.bootstrapper.proj')">
@@ -82,7 +105,11 @@
82105
<PaketRestoreRequired Condition=" '$(PaketRestoreLockFileHash)' == '' ">true</PaketRestoreRequired>
83106
</PropertyGroup>
84107

85-
<PropertyGroup Condition="'$(PaketPropsVersion)' != '5.174.2' ">
108+
<!--
109+
This value should match the version in the props generated by paket
110+
If they differ, this means we need to do a restore in order to ensure correct dependencies
111+
-->
112+
<PropertyGroup Condition="'$(PaketPropsVersion)' != '5.185.3' ">
86113
<PaketRestoreRequired>true</PaketRestoreRequired>
87114
</PropertyGroup>
88115

@@ -163,6 +190,7 @@
163190
<ExcludeAssets Condition=" '%(PaketReferencesFileLinesInfo.Splits)' == '6' And %(PaketReferencesFileLinesInfo.CopyLocal) == 'false'">runtime</ExcludeAssets>
164191
<ExcludeAssets Condition=" '%(PaketReferencesFileLinesInfo.Splits)' != '6' And %(PaketReferencesFileLinesInfo.AllPrivateAssets) == 'exclude'">runtime</ExcludeAssets>
165192
<Publish Condition=" '$(PackAsTool)' == 'true' ">true</Publish>
193+
<AllowExplicitVersion>true</AllowExplicitVersion>
166194
</PackageReference>
167195
</ItemGroup>
168196

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ open FsLibLog.Types
5555

5656
There are currently three ways to get a logger.
5757

58-
- `getCurrentLogger` - Creates a logger. It's name is based on the current StackFrame.
58+
- `getCurrentLogger` - __Deprecated__ because inferring the correct StackFrame is too difficult. Creates a logger. It's name is based on the current StackFrame.
59+
- `getLoggerByFunc` - Creates a logger based on `Reflection.MethodBase.GetCurrentMethod` call. This is only useful for calls within functions.
60+
- `getLoggerByQuotation` - Creates a logger given a Quotations.Expr type. This is only useful for module level declarations.
5961
- `getLoggerFor` - Creates a logger given a `'a` type.
6062
- `getLoggerByType` - Creates a logger given a `Type`.
6163
- `getLoggerByName` - Creates a logger given a `string`.

examples/SomeLib/Library.fs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ open FsLibLog
33
open FsLibLog.Types
44

55
module Say =
6-
let logger = LogProvider.getCurrentLogger()
7-
6+
// let logger = LogProvider.getLoggerByName "SomeLibrary.Say"
7+
let rec logger = LogProvider.getLoggerByQuotation <@ logger @>
88
type AdditionalData = {
99
Name : string
1010
}
1111

12-
12+
let rec myModule = LogProvider.getModuleType <@ myModule @>
1313
// Example Log Output:
1414
// 16:23 [Information] <SomeLib.Say> () "Captain" Was said hello to - {"UserContext": {"Name": "User123", "$type": "AdditionalData"}, "FunctionName": "hello"}
1515
let hello name =
16+
let logger2 = LogProvider.getLoggerByFunc ()
1617
// Starts the log out as an Informational log
17-
logger.info(
18+
logger2.info(
1819
Log.setMessage "{name} Was said hello to"
1920
// MessageTemplates require the order of parameters to be consistent with the tokens to replace
2021
>> Log.addParameter name

src/FsLibLog/FsLibLog.fs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ module LogProvider =
657657
open Types
658658
open Providers
659659
open System.Diagnostics
660+
open Microsoft.FSharp.Quotations.Patterns
660661

661662
let mutable private currentLogProvider = None
662663

@@ -792,14 +793,61 @@ module LogProvider =
792793
let getLoggerFor<'a> () =
793794
getLoggerByType(typeof<'a>)
794795

796+
let rec getModuleType =
797+
function
798+
| PropertyGet (_, propertyInfo, _) -> propertyInfo.DeclaringType
799+
// | Call (_, methInfo, _) -> sprintf "%s.%s" methInfo.DeclaringType.FullName methInfo.Name
800+
// | Lambda(_, expr) -> getModuleType expr
801+
// | ValueWithName(_,_,instance) -> instance
802+
| x -> failwithf "Expression is not a property. %A" x
803+
795804
/// **Description**
796805
///
797-
/// Creates a logger. It's name is based on the current StackFrame. This will attempt to retrieve any loggers set with `setLoggerProvider`. It will fallback to a known list of providers.
798-
/// WARNING: This has inconsisent results.
806+
/// Creates a logger given a Quotations.Expr type. This is only useful for module level declarations. It uses the DeclaringType on the PropertyInfo of the PropertyGet.
807+
///
808+
/// It can be utilized like:
809+
///
810+
/// `let rec logger = LogProvider.getLoggerByQuotation <@ logger @>`
811+
///
812+
/// inside a module to get the modules full qualitfied name.
813+
///
814+
/// **Parameters**
815+
/// * `quotation` - parameter of type `Quotations.Expr`
799816
///
800817
/// **Output Type**
801818
/// * `ILog`
802819
///
820+
/// **Exceptions**
821+
///
822+
let getLoggerByQuotation (quotation : Quotations.Expr) =
823+
getModuleType quotation
824+
|> getLoggerByType
825+
826+
827+
828+
/// **Description**
829+
///
830+
/// Creates a logger based on `Reflection.MethodBase.GetCurrentMethod` call. This is only useful for calls within functions. This does not protect against inlined functions.
831+
///
832+
/// **Output Type**
833+
/// * `ILog`
834+
///
835+
/// **Exceptions**
836+
///
837+
let inline getLoggerByFunc () =
838+
let mi = Reflection.MethodBase.GetCurrentMethod()
839+
sprintf "%s.%s" mi.DeclaringType.FullName mi.Name
840+
|> getLoggerByName
841+
842+
843+
/// **Description**
844+
///
845+
/// Creates a logger. It's name is based on the current StackFrame. This will attempt to retrieve any loggers set with `setLoggerProvider`. It will fallback to a known list of providers.
846+
/// Obsolete: getCurrentLogger is obsolete, choose another provider factory function.
847+
///
848+
/// **Output Type**
849+
/// * `ILog`
850+
[<Obsolete("getCurrentLogger is obsolete, choose another provider factory function")>]
803851
let getCurrentLogger () =
804852
let stackFrame = StackFrame(2, false)
805853
getLoggerByType(stackFrame.GetMethod().DeclaringType)

0 commit comments

Comments
 (0)