Last active
July 14, 2019 23:00
-
-
Save haskellcamargo/d2ca5bf6f5220d09411428ae9dfea28d to your computer and use it in GitHub Desktop.
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
#include 'protheus.ch' | |
// ANSI/VT-100 sequences for console formatting | |
#define ANSI_BOLD Chr( 27 ) + '[1m' | |
#define ANSI_LIGHT_RED Chr( 27 ) + '[91m' | |
#define ANSI_LIGHT_GREEN Chr( 27 ) + '[92m' | |
#define ANSI_LIGHT_YELLOW Chr( 27 ) + '[93m' | |
#define ANSI_CYAN Chr( 27 ) + '[36m' | |
#define ANSI_LIGHT_GRAY Chr( 27 ) + '[37m' | |
#define ANSI_LIGHT_MAGENTA Chr( 27 ) + '[95m' | |
#define ANSI_END Chr( 27 ) + '[0m' | |
Static Function Now() | |
Return ANSI_BOLD + '[' + DToC( Date() ) + ' ' + Time() + ']' + ANSI_END | |
Static Function FormatString( cInput, aValues ) | |
Local nIndex | |
Local cResult := cInput | |
Default aValues := {} | |
For nIndex := 1 To Len( aValues ) | |
cResult := StrTran( cResult, '{' + AllTrim( Str( nIndex ) ) + '}', cValToChar( aValues[ nIndex ] ) ) | |
Next | |
Return cResult | |
/** | |
* @class Logger | |
* @author Marcelo Camargo | |
* @since 02/2018 | |
**/ | |
Class Logger | |
Method New() Constructor | |
Method Log( cMessage, aValues ) | |
Method Info( cMessage, aValues ) | |
Method Error( cMessage, aValues ) | |
Method Warn( cMessage, aValues ) | |
Method Success( cMessage, aValues ) | |
EndClass | |
Method New() Class Logger | |
Return Self | |
Method Log( cMessage, aValues ) Class Logger | |
ConOut( Now() + ' [LOG] ' + ANSI_LIGHT_GRAY + FormatString( cMessage, aValues ) + ANSI_END ) | |
Return Self | |
Method Info( cMessage, aValues ) Class Logger | |
ConOut( Now() + ' [INFO] ' + ANSI_CYAN + FormatString( cMessage, aValues ) + ANSI_END ) | |
Return Self | |
Method Error( cMessage, aValues ) Class Logger | |
ConOut( Now() + ' [ERROR] ' + ANSI_LIGHT_RED + FormatString( cMessage, aValues ) + ANSI_END ) | |
Return Self | |
Method Warn( cMessage, aValues ) Class Logger | |
ConOut( Now() + ' [WARN] ' + ANSI_LIGHT_YELLOW + FormatString( cMessage, aValues ) + ANSI_END ) | |
Return Self | |
Method Success( cMessage, aValues ) Class Logger | |
ConOut( Now() + ' [SUCCESS] ' + ANSI_LIGHT_GREEN + FormatString( cMessage, aValues ) + ANSI_END ) | |
Return Self | |
User Function TestLogger | |
Local nCounter | |
Local nRandom | |
Local aMethods | |
Local aRoutes | |
Local cMessage | |
Local oLogger := Logger():New() | |
oLogger:Log( 'Starting logger service' ) | |
oLogger:Info( 'Rest started on port {1}', { 8080 } ) | |
oLogger:Error( '500 internal server error {1} - {2} {1} {45}', { 'EITA', 'PREULA', 'FOO' } ) | |
oLogger:Warn( 'GET on /login is deprecated' ) | |
oLogger:Success( 'Loved all capybaras!' ) | |
aMethods := { 'GET', 'POST', 'PUT', 'PATCH', 'DELETE' } | |
aRoutes := { '/login', '/users', '/user/10', '/capybaras', '/version', '/help' } | |
For nCounter := 1 To 100 | |
cMessage := { aMethods[ Randomize( 1, 5 ) ], aRoutes[ Randomize( 1, 6 ) ] } | |
nRandom := Randomize( 1, 15 ) | |
If nRandom == 6 | |
oLogger:Error( '{1} {2}', cMessage ) | |
ElseIf nRandom == 5 .Or. nRandom == 9 | |
oLogger:Warn( '{1} {2}', cMessage ) | |
Else | |
oLogger:Info( '{1} {2}', cMessage ) | |
EndIf | |
Sleep( Randomize( 150, 450 ) ) | |
Next | |
Return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Boa tarde,
Como você faz para mudar a cor do texto no console do appserver?
Testei esse seu teste na minha base e não faz a substituição dos caracteres do ansi.