VMware vSphere Automation SDK Perl The VMware vSphere Automation SDK for Perl supports the client-side vSphere Automation API for access to vSphere services and capabilities

- Main Page
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

All Classes Files Functions
Public Attributes | List of all members
Com::Vmware::Vapi::Util::Logger Class Reference

## Public Attributes
scalar | FAILURE
scalar | SUCCESS
hash | verbosity

## Avaliable Methods
private method | writeToScreen ()
private method | writeLog ()
private method | open_log ()
public method | set_verbosity ()
public method | new_instance ()
public method | InstanceExists ()
public method | get_log_name ()
public method | SetChannelLength ()
public method | get_instance ()
public method | log_dumper ()
public method | log_verbose ()
public method | get_logpath ()
public method | close_log ()
public method | CallingPackage ()
public method | getCurrentTime ()
public method | set_logpath ()
public method | get_zip_path ()
public method | log_framework ()
public method | prepareLogs ()
public method | CallingFunction ()
public method | log_info ()
public method | log_error ()
public method | formatString ()
public method | log_warning ()
public method | log_debug ()

## Detailed Description

This class lets you control the logging behavior. It allows you to control logging messages written to the screen through various verbosity level (0-7) You could change the level or turn off logging capability to the screen anytime.

Assumptions:
  None.

Dependencies:
  None.

      # use Com::Vmware::Vapi::Util::Logger qw(log_info log_error);
      # log_error(MSG=>"Proceed with caution...");
      # set_logpath(XMLFile=>"Test.Logger.First.xml");
      # log_info(Channel=> "mylogs", MSG=>"VAPI Perl SDK Rocks...Way to go");

## Member Function Documentation

public method Com::Vmware::Vapi::Util::Logger::CallingFunction | ( |  | ) |

Return name of calling function

Return values

  SUCCESS or exception on failure. |

Side Effects:
  None

      # CallingPackage

Code:
click to view
# Number of lines of code in CallingFunction: 16
sub CallingFunction
{
my ($frame) = @_;
$frame = 2 if (! defined $frame);
foreach $frame ($frame .. 10) {
my $caller = (caller ($frame)) [3];
if ($caller ne '(eval)') {
my ($package, $func) = $caller =~ m/^(.+)::([^:]+)$/;
return $caller if ($func ne 'eval');
}
}
}

public method Com::Vmware::Vapi::Util::Logger::CallingPackage | ( |  | ) |

Return name of calling package

Return values

  SUCCESS or exception on failure. |

Side Effects:
  None

      # CallingPackage

Code:
click to view
# Number of lines of code in CallingPackage: 20
sub CallingPackage
{
my ($frame) = @_;
$frame = 2 if (! defined $frame);
foreach $frame ($frame .. 10) {
my $caller = (caller ($frame)) [3];
if (defined $caller) {
if ($caller ne '(eval)') {
my ($package, $func) = $caller =~ m/^(.+)::([^:]+)$/;
return $package if ($func ne 'eval');
}
} else {
return 'unknown';
}
}
}

public method Com::Vmware::Vapi::Util::Logger::close_log | ( |  | ) |

A private method to close the log file in the launch host

Parameters

  None |

Return values

  None |

Side Effects:
  None

      # close_log();

Code:
click to view
# Number of lines of code in close_log: 11
sub close_log {
my $self = get_instance();
my $string = "close_log call FAILED. Test Log has not been opened";
if (defined $self->{fileHandle} && defined $self->{logFile}) {
close $self->{fileHandle};
$self->{handle} = undef;
} else {
throw LoggerException("$string");
}
}

public method Com::Vmware::Vapi::Util::Logger::formatString | ( |  | ) |

A private method to customize number of characters in a string and return string removing charcters more than the length specified or add white spaces accordingly.

Parameters

  String | - string name

Return values

  Formatted String |

Side Effects:
  None

      # formatString(String => "ChangeString");

Code:
click to view
# Number of lines of code in formatString: 17
sub formatString {
my %args = @_;
my $string = $args{String};
my $numOfChar = length($string);
my $self = get_instance();
my $whiteSpace = undef;
if ($numOfChar < $self->{channelLength}) {
my $diff = $self->{channelLength} - $numOfChar;
for (my $i = 0; $i < $diff; $i++) {
$whiteSpace = $whiteSpace . " ";
}
$string = $string . $whiteSpace;
} else {
$string = substr($string, 0, $self->{channelLength});
}
return $string;
}

public method Com::Vmware::Vapi::Util::Logger::get_instance | ( |  | ) |

This method is called to get the current object instance or create a new one by calling new_instance().

Return values

  An object instance |

Side Effects:
  None

      # my $instance = get_instance()

Code:
click to view
# Number of lines of code in get_instance: 11
sub get_instance {
my $class = __PACKAGE__;
#
# return $class if ref $class;
# instance is stored in the _instance variable in the $class package
#
my $instance = \${ "$class\::_instance" };
defined $$instance
? $$instance
: ($$instance = $class->new_instance(@_));
}

public method Com::Vmware::Vapi::Util::Logger::get_log_name | ( |  | ) |

A public method which returns the path to log file

Parameters

  None |

Return values

  None |

Side Effects:
  None

      # get_log_name();

Code:
click to view
# Number of lines of code in get_log_name: 5
sub get_log_name
{
my $self = get_instance();
return $self->{logFile};
}

public method Com::Vmware::Vapi::Util::Logger::get_logpath | ( |  | ) |

A public method which returns the path to log direcory

Parameters

  None |

Return values

  None |

Side Effects:
  None

      # get_logpath();

Code:
click to view
# Number of lines of code in get_logpath: 5
sub get_logpath
{
my $self = get_instance();
return $self->{logDir};
}

public method Com::Vmware::Vapi::Util::Logger::get_zip_path | ( |  | ) |

A public method which returns the path to zip direcory

Parameters

  None |

Return values

  None |

Side Effects:
  None

      # get_zip_path();

Code:
click to view
# Number of lines of code in get_zip_path: 5
sub get_zip_path
{
my $self = get_instance();
return $self->{zipDir};
}

public method Com::Vmware::Vapi::Util::Logger::getCurrentTime | ( |  | ) |

A private method to get current time stamp

Parameters

  None |

Return values

  None |

Side Effects:
  None

      # getCurrentTime();

Code:
click to view
# Number of lines of code in getCurrentTime: 4
sub getCurrentTime {
my $currentTime = localtime();
return $currentTime;
}

public method Com::Vmware::Vapi::Util::Logger::InstanceExists | ( |  | ) |

A Public method to return the current instance if it exist

Return values

  An object instance |

Side Effects:
  None

      # my $instance = InstanceExists()

Code:
click to view
# Number of lines of code in InstanceExists: 5
sub InstanceExists {
my ($class) = @_;
$class = ref $class || $class;
return ${"$class\::_instance"};
}

public method Com::Vmware::Vapi::Util::Logger::log_debug | ( |  | ) |

A Public method to print log debug message

Return values

  None |

Side Effects:
  None

      # log_debug(MSG=>"This is a debug message");

Code:
click to view
# Number of lines of code in log_debug: 3
sub log_debug {
writeLog (@_, Type => 'DEBUG');
}

public method Com::Vmware::Vapi::Util::Logger::log_dumper | ( |  | ) |

A Public method to print Data:Dumper for the given data type

Return values

  None |

Side Effects:
  None

      # LogFramework (MSG=>"This is a debug message");

Code:
click to view
# Number of lines of code in log_dumper: 4
sub log_dumper {
my $args = Dumper(@_);
writeLog (MSG => $args, Type => 'DUMPER');
}

public method Com::Vmware::Vapi::Util::Logger::log_error | ( |  | ) |

A Public method to print log error message

Return values

  None |

Side Effects:
  None

      # log_error(MSG=>"This is a error message");

Code:
click to view
# Number of lines of code in log_error: 3
sub log_error {
writeLog (@_, Type => 'ERROR');
}

public method Com::Vmware::Vapi::Util::Logger::log_framework | ( |  | ) |

A Public method to print log framework debug messages

Return values

  None |

Side Effects:
  None

      # LogFramework (MSG=>"This is a debug message");

Code:
click to view
# Number of lines of code in log_framework: 4
sub log_framework {
my $args = Dumper(@_);
writeLog (MSG => $args, Type => 'FRAMEWORK');
}

public method Com::Vmware::Vapi::Util::Logger::log_info | ( |  | ) |

A Public method to print log info message

Return values

  None |

Side Effects:
  None

      # log_info(MSG=>"This is a info message");

Code:
click to view
# Number of lines of code in log_info: 3
sub log_info {
writeLog (@_, Type => 'INFO');
}

public method Com::Vmware::Vapi::Util::Logger::log_verbose | ( |  | ) |

A Public method to print log info message

Return values

  None |

Side Effects:
  None

      # log_verbose(MSG=>"This is a info message");

Code:
click to view
# Number of lines of code in log_verbose: 3
sub log_verbose {
writeLog (@_, Type => 'VERBOSE');
}

public method Com::Vmware::Vapi::Util::Logger::log_warning | ( |  | ) |

A Public method to print log warning message

Return values

  None |

Side Effects:
  None

      # log_warning(MSG=>"This is a warning message");

Code:
click to view
# Number of lines of code in log_warning: 4
sub log_warning {
my $args = Dumper(@_);
writeLog (MSG => $args, Type => 'WARN');
}

public method Com::Vmware::Vapi::Util::Logger::new_instance | ( |  | ) |

Undocumented Method

Code:
click to view
# Number of lines of code in new_instance: 15
sub new_instance {
my ($class) = @_;
my $self = {};
$self->{logFile} = undef;
$self->{logPath} = undef;
$self->{fileHandle} = undef;
$self->{temp} = "/tmp";
$self->{verbosity} = 3;
$self->{channelLength} = 10;
bless ($self, $class);
select(STDOUT); $| = 1; # unbuffered io
return $self;
}

private method Com::Vmware::Vapi::Util::Logger::open_log | ( |  | ) |

A private method to open a log file in the launch host

Return values

  None |

Side Effects:
  None

      # open_log();

Code:
click to view
# Number of lines of code in open_log: 7
sub open_log {
my $self = get_instance();
my $string = "Logger: open_log: Failed to open file $self->{logFile}";
unless ( open $self->{fileHandle}, ">>", $self->{logFile}) {
throw LoggerException("$string");
}
}

public method Com::Vmware::Vapi::Util::Logger::prepareLogs | ( |  | ) |

A private method to prepare test and log path with the time stamp

Parameters

  None |

Return values

  None |

Side Effects:
  None

      # prepareLogs();

Code:
click to view
# Number of lines of code in prepareLogs: 4
sub prepareLogs {
my $self = get_instance();
$self->{logFile} = $self->{xmlFile};
}

public method Com::Vmware::Vapi::Util::Logger::set_logpath | ( |  | ) |

A public method to get the XMLFile information and creates log directories

Parameters

  XMLFile | - Full path to the XML file

Return values

  None |

Side Effects:
  None

      # set_logpath (XMLFile => "/source/Proj/ctd/unreviewed/TestLogger.xml");

Code:
click to view
# Number of lines of code in set_logpath: 6
sub set_logpath {
my (%args) = @_;
$self = get_instance();
$self->{xmlFile} = $args{XMLFile};
prepareLogs();
}

public method Com::Vmware::Vapi::Util::Logger::set_verbosity | ( |  | ) |

A public method to set verbosity for the logger object

Parameters

  Level | Verbosity Level

Return values

  None |

Side Effects:
  None

      # set_verbosity(level => 3);

Code:
click to view
# Number of lines of code in set_verbosity: 5
sub set_verbosity {
my %args = @_;
my $self = get_instance();
$self->{verbosity} = $args{level} > 7 ? 7 : $args{level};
}

public method Com::Vmware::Vapi::Util::Logger::SetChannelLength | ( |  | ) |

A public method to set number of character in a channel string.

Parameters

  Length | - Length of the channel

Return values

  None |

Side Effects:
  None

      # SetChannelLength(Length => 10);

Code:
click to view
# Number of lines of code in SetChannelLength: 5
sub SetChannelLength {
my %args = @_;
my $self = get_instance();
$self->{channelLength} = $args{Length};
}

private method Com::Vmware::Vapi::Util::Logger::writeLog | ( |  | ) |

A private method to log messages to log file and the screen

Parameters

  MSG | - Message
  Type | - Message Type (Info, Warn...)
  Channel | - Channel Name

Return values

  None |

Side Effects:
  None

      #   writeLog(Channel => $args{Channel},
      #            MSG => $args{MSG},
      #            Type => "DEBUG");

Code:
click to view
# Number of lines of code in writeLog: 47
sub writeLog {
my %args = @_;
my $self = get_instance();
my $string = $args{MSG};
my $type = $args{Type};
$type =~ s/\s+$//;
#
# Don't log if the verbosity level is lower
if ($self->{verbosity} < $verbosity {$type}) {
return;
}
#
# Set it to the default channel
#
if (defined $args{Channel}) {
$channel = uc ($args{Channel});
} else {
$channel = uc (CallingPackage(3));
($channel) = $channel =~ m/::([^:]+)$/ if ($channel =~ /::/);
}
#$channel = formatString(String => $channel);
my $timeStamp = getCurrentTime();
#
# Logs to STDOUT and STDERR based on the log type
#
$self->writeToScreen (Channel => $channel,
TimeStamp => $timeStamp,
Type => $type,
String => $string);
#
# Writing it to the log file if log file is defined
#
if (defined $self->{logFile}) {
open_log() ;
my $handle = $self->{fileHandle};
print $handle "[$timeStamp: $channel] [" . threads->tid() . "] $type: $string\n";
}
# If log file is defined, close it
if (defined $self->{logFile}) {
close_log();
}
}

private method Com::Vmware::Vapi::Util::Logger::writeToScreen | ( |  | ) |

A private method to write messages to the screen

Parameters

  Channel | - Channel Name
  Type | - Message Type
  TimeStamp | - TimeStamp details
  String | - Message String

Return values

  None |

Side Effects:
  None

      #      writeToScreen(Channel => $channel, TimeStamp => $timeStamp,
      #                    Type => $type, String => $string);

Code:
click to view
# Number of lines of code in writeToScreen: 32
sub writeToScreen
{
my ($self, %args) = @_;
if (! exists $self->{Monochrome}) {
$self->{Monochrome} = 1;
}
my $string = $args{String};
my $type = $args{Type};
$type =~ s/\s+$//;
my $timeStamp = $args{TimeStamp};
my $channel = uc ($args{Channel});
#
# write to screen based on the color type if exists
#
if (exists $colorBox{$type} ) {
print Term::ANSIColor::color( $colorBox{$type} )
if (! $self->{Monochrome});
print "\n[$timeStamp: $channel] [" . threads->tid() . "] $type: $string";
print RESET
if (! $self->{Monochrome});
} else {
print "\n[$timeStamp: $channel] [" . threads->tid() . "] $type: $string";
}
}

The documentation for this class was generated from the following file:

- Com/Vmware/Vapi/Util/Logger.pm