Skip to content

Commit

Permalink
Added CliLogger for pretty formated logging
Browse files Browse the repository at this point in the history
  • Loading branch information
alfhen committed Dec 21, 2015
1 parent d393c4e commit 5101923
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ composer require alfhen/php-shell

## Usage

### CliArgsParser:

When creating a php shell script you can use the ```CliArgsParser``` to parse arguments passed to your shell scripts.
The parser expects arguments to be passed in following format --argument='<value>' note you should not write <>
but just the raw value inside the quotes.
Expand All @@ -26,4 +28,21 @@ Each argument should be separated with a space.
$parsedArguments = $argsParser->getArguments();

```
### CliLogger:
CliLogger is a tool that helps you print nicely formated lines to the terminal/console. It comes with three built in types:
Info, Error and Success. It can be used as such:

``` php

Alfhen\PhpShell\CliLogger::info('This is an info message');

Alfhen\PhpShell\CliLogger::error('This is an error message');

Alfhen\PhpShell\CliLogger::success('This is a success message');


```



Happy coding and thanks for using the tools
52 changes: 52 additions & 0 deletions src/CliLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Alfhen\PhpShell;

class CliLogger
{
const CLI_COLOR_RED = "\033[31m";
const CLI_COLOR_GREEN = "\033[32m";
const CLI_COLOR_DEFAULT = "\033[0m";
const CLI_TYPE_ERROR = 'Error';
const CLI_TYPE_INFO = 'Info';
const CLI_TYPE_SUCCESS = 'Success';

/**
* Write a message of type Error to terminal
*
* @param string $message
*/
public static function error($message = '') {
self::writeLine($message, SELF::CLI_COLOR_RED, self::CLI_TYPE_ERROR);
}

/**
* Write a message of type info to terminal
*
* @param string $message
*/
public static function info($message = '') {
self::writeLine($message, SELF::CLI_COLOR_DEFAULT, self::CLI_TYPE_ERROR);
}

/**
* Write a message of type success to terminal
*
* @param string $message
*/
public static function success($message = '') {
self::writeLine($message, SELF::CLI_COLOR_GREEN, self::CLI_TYPE_ERROR);
}

/**
* Write a line to termnial/console with provided params
*
* @param $message
* @param string $color
* @param string $type
*/
private static function writeLine($message, $color = self::CLI_COLOR_DEFAULT, $type = self::CLI_TYPE_INFO) {
$defaultColor = self::CLI_COLOR_DEFAULT;
echo "$color $type:$defaultColor $message\n";
}
}

0 comments on commit 5101923

Please sign in to comment.