From a6db65a70140324231864e7a1bb49f8500b3661c Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Sun, 25 Feb 2018 14:46:41 +0100 Subject: [PATCH] Imported Upstream version 0.96 --- Build.PL | 12 + Changes | 31 ++ LICENSE | 378 +++++++++++++++++++++++ MANIFEST | 16 + META.json | 85 ++++++ META.yml | 48 +++ README.md | 243 +++++++++++++++ cpanfile | 14 + lib/Log/GELF/Util.pm | 698 +++++++++++++++++++++++++++++++++++++++++++ minil.toml | 4 + t/00_compile.t | 9 + t/01_utilities.t | 147 +++++++++ t/02_validate.t | 174 +++++++++++ t/03_encode.t | 60 ++++ t/04_compress.t | 116 +++++++ t/05_chunked.t | 186 ++++++++++++ 16 files changed, 2221 insertions(+) create mode 100644 Build.PL create mode 100644 Changes create mode 100644 LICENSE create mode 100644 MANIFEST create mode 100644 META.json create mode 100644 META.yml create mode 100644 README.md create mode 100644 cpanfile create mode 100644 lib/Log/GELF/Util.pm create mode 100644 minil.toml create mode 100644 t/00_compile.t create mode 100644 t/01_utilities.t create mode 100644 t/02_validate.t create mode 100644 t/03_encode.t create mode 100644 t/04_compress.t create mode 100644 t/05_chunked.t diff --git a/Build.PL b/Build.PL new file mode 100644 index 0000000..16855c4 --- /dev/null +++ b/Build.PL @@ -0,0 +1,12 @@ +# ========================================================================= +# THIS FILE IS AUTOMATICALLY GENERATED BY MINILLA. +# DO NOT EDIT DIRECTLY. +# ========================================================================= + +use 5.008_001; +use strict; + +use Module::Build::Tiny 0.035; + +Build_PL(); + diff --git a/Changes b/Changes new file mode 100644 index 0000000..5c0e5bb --- /dev/null +++ b/Changes @@ -0,0 +1,31 @@ +Revision history for Perl extension Log-GELF-Util + +0.96 2016-03-01T12:49:34Z + + - added ability to specify message id when chunking + +0.95 2016-02-26T00:26:50Z + + - improve documentaion + - make use of GELF message magic constant in enchunk() + +0.94 2016-02-25T12:16:02Z + + - facility was incorrectly limited to numbers + +0.93 2016-02-25T06:04:28Z + + - documentation cleanups + +0.92 2016-02-24T23:22:01Z + + - fixed bug introduced with Readonly.pm v2.01 + +0.91 2016-02-24T22:47:40Z + + - dechunk() now handles chunks being passed in random order + +0.90 2016-02-24T04:55:07Z + + - original version + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4546314 --- /dev/null +++ b/LICENSE @@ -0,0 +1,378 @@ +This software is copyright (c) 2016 by Adam Clarke . + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +Terms of the Perl programming language system itself + +a) the GNU General Public License as published by the Free + Software Foundation; either version 1, or (at your option) any + later version, or +b) the "Artistic License" + +--- The GNU General Public License, Version 1, February 1989 --- + +This software is Copyright (c) 2016 by Adam Clarke . + +This is free software, licensed under: + + The GNU General Public License, Version 1, February 1989 + + GNU GENERAL PUBLIC LICENSE + Version 1, February 1989 + + Copyright (C) 1989 Free Software Foundation, Inc. + 51 Franklin St, Suite 500, Boston, MA 02110-1335 USA + + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The license agreements of most software companies try to keep users +at the mercy of those companies. By contrast, our General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. The +General Public License applies to the Free Software Foundation's +software and to any other program whose authors commit to using it. +You can use it for your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Specifically, the General Public License is designed to make +sure that you have the freedom to give away or sell copies of free +software, that you receive source code or can get it if you want it, +that you can change the software or use pieces of it in new free +programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of a such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must tell them their rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any program or other work which +contains a notice placed by the copyright holder saying it may be +distributed under the terms of this General Public License. The +"Program", below, refers to any such program or work, and a "work based +on the Program" means either the Program or any work containing the +Program or a portion of it, either verbatim or with modifications. Each +licensee is addressed as "you". + + 1. You may copy and distribute verbatim copies of the Program's source +code as you receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice and +disclaimer of warranty; keep intact all the notices that refer to this +General Public License and to the absence of any warranty; and give any +other recipients of the Program a copy of this General Public License +along with the Program. You may charge a fee for the physical act of +transferring a copy. + + 2. You may modify your copy or copies of the Program or any portion of +it, and copy and distribute such modifications under the terms of Paragraph +1 above, provided that you also do the following: + + a) cause the modified files to carry prominent notices stating that + you changed the files and the date of any change; and + + b) cause the whole of any work that you distribute or publish, that + in whole or in part contains the Program or any part thereof, either + with or without modifications, to be licensed at no charge to all + third parties under the terms of this General Public License (except + that you may choose to grant warranty protection to some or all + third parties, at your option). + + c) If the modified program normally reads commands interactively when + run, you must cause it, when started running for such interactive use + in the simplest and most usual way, to print or display an + announcement including an appropriate copyright notice and a notice + that there is no warranty (or else, saying that you provide a + warranty) and that users may redistribute the program under these + conditions, and telling the user how to view a copy of this General + Public License. + + d) You may charge a fee for the physical act of transferring a + copy, and you may at your option offer warranty protection in + exchange for a fee. + +Mere aggregation of another independent work with the Program (or its +derivative) on a volume of a storage or distribution medium does not bring +the other work under the scope of these terms. + + 3. You may copy and distribute the Program (or a portion or derivative of +it, under Paragraph 2) in object code or executable form under the terms of +Paragraphs 1 and 2 above provided that you also do one of the following: + + a) accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of + Paragraphs 1 and 2 above; or, + + b) accompany it with a written offer, valid for at least three + years, to give any third party free (except for a nominal charge + for the cost of distribution) a complete machine-readable copy of the + corresponding source code, to be distributed under the terms of + Paragraphs 1 and 2 above; or, + + c) accompany it with the information you received as to where the + corresponding source code may be obtained. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form alone.) + +Source code for a work means the preferred form of the work for making +modifications to it. For an executable file, complete source code means +all the source code for all modules it contains; but, as a special +exception, it need not include source code for modules which are standard +libraries that accompany the operating system on which the executable +file runs, or for standard header files or definitions files that +accompany that operating system. + + 4. You may not copy, modify, sublicense, distribute or transfer the +Program except as expressly provided under this General Public License. +Any attempt otherwise to copy, modify, sublicense, distribute or transfer +the Program is void, and will automatically terminate your rights to use +the Program under this License. However, parties who have received +copies, or rights to use copies, from you under this General Public +License will not have their licenses terminated so long as such parties +remain in full compliance. + + 5. By copying, distributing or modifying the Program (or any work based +on the Program) you indicate your acceptance of this license to do so, +and all its terms and conditions. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the original +licensor to copy, distribute or modify the Program subject to these +terms and conditions. You may not impose any further restrictions on the +recipients' exercise of the rights granted herein. + + 7. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of the license which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +the license, you may choose any version ever published by the Free Software +Foundation. + + 8. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 9. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 10. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to humanity, the best way to achieve this is to make it +free software which everyone can redistribute and change under these +terms. + + To do so, attach the following notices to the program. It is safest to +attach them to the start of each source file to most effectively convey +the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 1, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19xx name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the +appropriate parts of the General Public License. Of course, the +commands you use may be called something other than `show w' and `show +c'; they could even be mouse-clicks or menu items--whatever suits your +program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + program `Gnomovision' (a program to direct compilers to make passes + at assemblers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +That's all there is to it! + + +--- The Artistic License 1.0 --- + +This software is Copyright (c) 2016 by Adam Clarke . + +This is free software, licensed under: + + The Artistic License 1.0 + +The Artistic License + +Preamble + +The intent of this document is to state the conditions under which a Package +may be copied, such that the Copyright Holder maintains some semblance of +artistic control over the development of the package, while giving the users of +the package the right to use and distribute the Package in a more-or-less +customary fashion, plus the right to make reasonable modifications. + +Definitions: + + - "Package" refers to the collection of files distributed by the Copyright + Holder, and derivatives of that collection of files created through + textual modification. + - "Standard Version" refers to such a Package if it has not been modified, + or has been modified in accordance with the wishes of the Copyright + Holder. + - "Copyright Holder" is whoever is named in the copyright or copyrights for + the package. + - "You" is you, if you're thinking about copying or distributing this Package. + - "Reasonable copying fee" is whatever you can justify on the basis of media + cost, duplication charges, time of people involved, and so on. (You will + not be required to justify it to the Copyright Holder, but only to the + computing community at large as a market that must bear the fee.) + - "Freely Available" means that no fee is charged for the item itself, though + there may be fees involved in handling the item. It also means that + recipients of the item may redistribute it under the same conditions they + received it. + +1. You may make and give away verbatim copies of the source form of the +Standard Version of this Package without restriction, provided that you +duplicate all of the original copyright notices and associated disclaimers. + +2. You may apply bug fixes, portability fixes and other modifications derived +from the Public Domain or from the Copyright Holder. A Package modified in such +a way shall still be considered the Standard Version. + +3. You may otherwise modify your copy of this Package in any way, provided that +you insert a prominent notice in each changed file stating how and when you +changed that file, and provided that you do at least ONE of the following: + + a) place your modifications in the Public Domain or otherwise make them + Freely Available, such as by posting said modifications to Usenet or an + equivalent medium, or placing the modifications on a major archive site + such as ftp.uu.net, or by allowing the Copyright Holder to include your + modifications in the Standard Version of the Package. + + b) use the modified Package only within your corporation or organization. + + c) rename any non-standard executables so the names do not conflict with + standard executables, which must also be provided, and provide a separate + manual page for each non-standard executable that clearly documents how it + differs from the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +4. You may distribute the programs of this Package in object code or executable +form, provided that you do at least ONE of the following: + + a) distribute a Standard Version of the executables and library files, + together with instructions (in the manual page or equivalent) on where to + get the Standard Version. + + b) accompany the distribution with the machine-readable source of the Package + with your modifications. + + c) accompany any non-standard executables with their corresponding Standard + Version executables, giving the non-standard executables non-standard + names, and clearly documenting the differences in manual pages (or + equivalent), together with instructions on where to get the Standard + Version. + + d) make other distribution arrangements with the Copyright Holder. + +5. You may charge a reasonable copying fee for any distribution of this +Package. You may charge any fee you choose for support of this Package. You +may not charge a fee for this Package itself. However, you may distribute this +Package in aggregate with other (possibly commercial) programs as part of a +larger (possibly commercial) software distribution provided that you do not +advertise this Package as a product of your own. + +6. The scripts and library files supplied as input to or produced as output +from the programs of this Package do not automatically fall under the copyright +of this Package, but belong to whomever generated them, and may be sold +commercially, and may be aggregated with this Package. + +7. C or perl subroutines supplied by you and linked into this Package shall not +be considered part of this Package. + +8. The name of the Copyright Holder may not be used to endorse or promote +products derived from this software without specific prior written permission. + +9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF +MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. + +The End diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..d067b2e --- /dev/null +++ b/MANIFEST @@ -0,0 +1,16 @@ +Build.PL +Changes +LICENSE +META.json +README.md +cpanfile +lib/Log/GELF/Util.pm +minil.toml +t/00_compile.t +t/01_utilities.t +t/02_validate.t +t/03_encode.t +t/04_compress.t +t/05_chunked.t +META.yml +MANIFEST \ No newline at end of file diff --git a/META.json b/META.json new file mode 100644 index 0000000..d74d909 --- /dev/null +++ b/META.json @@ -0,0 +1,85 @@ +{ + "abstract" : "Utility functions for Graylog's GELF format.", + "author" : [ + "Adam Clarke " + ], + "dynamic_config" : 0, + "generated_by" : "Minilla/v3.0.1", + "license" : [ + "perl_5" + ], + "meta-spec" : { + "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", + "version" : "2" + }, + "name" : "Log-GELF-Util", + "no_index" : { + "directory" : [ + "t", + "xt", + "inc", + "share", + "eg", + "examples", + "author", + "builder" + ] + }, + "prereqs" : { + "configure" : { + "requires" : { + "Module::Build::Tiny" : "0.035" + } + }, + "develop" : { + "requires" : { + "Test::CPAN::Meta" : "0", + "Test::MinimumVersion::Fast" : "0.04", + "Test::PAUSE::Permissions" : "0.04", + "Test::Pod" : "1.41", + "Test::Spellunker" : "v0.2.7" + } + }, + "runtime" : { + "requires" : { + "JSON::MaybeXS" : "0", + "Math::Random::MT" : "0", + "Params::Validate" : "0", + "Readonly" : "2.00", + "Sys::Hostname" : "0", + "Time::HiRes" : "0", + "perl" : "5.010" + } + }, + "test" : { + "requires" : { + "Test::Exception" : "0", + "Test::More" : "0.98", + "Test::Warnings" : "0.005" + } + } + }, + "provides" : { + "Log::GELF::Util" : { + "file" : "lib/Log/GELF/Util.pm", + "version" : "0.96" + } + }, + "release_status" : "stable", + "resources" : { + "bugtracker" : { + "web" : "https://github.com/strategicdata/log-gelf-util/issues" + }, + "homepage" : "https://github.com/strategicdata/log-gelf-util", + "repository" : { + "url" : "git://github.com/strategicdata/log-gelf-util.git", + "web" : "https://github.com/strategicdata/log-gelf-util" + } + }, + "version" : "0.96", + "x_contributors" : [ + "Adam Clarke ", + "Adam Clarke " + ], + "x_serialization_backend" : "JSON::PP version 2.27300" +} diff --git a/META.yml b/META.yml new file mode 100644 index 0000000..bac5a95 --- /dev/null +++ b/META.yml @@ -0,0 +1,48 @@ +--- +abstract: "Utility functions for Graylog's GELF format." +author: + - 'Adam Clarke ' +build_requires: + Test::Exception: '0' + Test::More: '0.98' + Test::Warnings: '0.005' +configure_requires: + Module::Build::Tiny: '0.035' +dynamic_config: 0 +generated_by: 'Minilla/v3.0.1, CPAN::Meta::Converter version 2.150005' +license: perl +meta-spec: + url: http://module-build.sourceforge.net/META-spec-v1.4.html + version: '1.4' +name: Log-GELF-Util +no_index: + directory: + - t + - xt + - inc + - share + - eg + - examples + - author + - builder +provides: + Log::GELF::Util: + file: lib/Log/GELF/Util.pm + version: '0.96' +requires: + JSON::MaybeXS: '0' + Math::Random::MT: '0' + Params::Validate: '0' + Readonly: '2.00' + Sys::Hostname: '0' + Time::HiRes: '0' + perl: '5.010' +resources: + bugtracker: https://github.com/strategicdata/log-gelf-util/issues + homepage: https://github.com/strategicdata/log-gelf-util + repository: git://github.com/strategicdata/log-gelf-util.git +version: '0.96' +x_contributors: + - 'Adam Clarke ' + - 'Adam Clarke ' +x_serialization_backend: 'CPAN::Meta::YAML version 0.012' diff --git a/README.md b/README.md new file mode 100644 index 0000000..afbbe17 --- /dev/null +++ b/README.md @@ -0,0 +1,243 @@ +[![Build Status](https://travis-ci.org/strategicdata/log-gelf-util.svg?branch=master)](https://travis-ci.org/strategicdata/log-gelf-util) +# NAME + +Log::GELF::Util - Utility functions for Graylog's GELF format. + +# SYNOPSIS + + use Log::GELF::Util qw( encode ); + + my $msg = encode( { short_message => 'message', } ); + + + use Log::GELF::Util qw( :all ); + + sub process_chunks { + + my @accumulator; + my $msg; + + do { + $msg = dechunk( + \@accumulator, + decode_chunk(shift()) + ); + } until ($msg); + + return uncompress($msg); + }; + + my $hr = validate_message( short_message => 'message' ); + +# DESCRIPTION + +Log::GELF::Util is a collection of functions and data structures useful +when working with Graylog's GELF Format version 1.1. It strives to support +all of the features and options as described in the [GELF +specification](http://docs.graylog.org/en/latest/pages/gelf.html). + +# FUNCTIONS + +## validate\_message( short\_message => $ ) + +Returns a HASHREF representing the validated message with any defaulted +values added to the data structure. + +Takes the following message parameters as per the GELF message +specification: + +- short\_message + + Mandatory string, a short descriptive message + +- version + + String, must be '1.1' which is the default. + +- host + + String, defaults to hostname() from [Sys::Hostname](https://metacpan.org/pod/Sys::Hostname). + +- timestamp + + Timestamp, defaults to time() from [Time::HiRes](https://metacpan.org/pod/Time::HiRes). + +- level + + Integer, equal to the standard syslog levels, default is 1 (ALERT). + +- facility + + Deprecated, a warning will be issued. + +- line + + Deprecated, a warning will be issued. + +- file + + Deprecated, a warning will be issued. + +- \_\[additional\_field\] + + Parameters prefixed with an underscore (\_) will be treated as an additional + field. Allowed characters in field names are any word character (letter, + number, underscore), dashes and dots. As per the specification '\_id' is + disallowed. + +## encode( \\% ) + +Accepts a HASHREF representing a GELF message. The message will be +validated with ["validate\_message"](#validate_message). + +Returns a JSON encoded string representing the message. + +## decode( $ ) + +Accepts a JSON encoded string representing the message. This will be +converted to a hashref and validated with ["validate\_message"](#validate_message). + +Returns a HASHREF representing the validated message with any defaulted +values added to the data structure. + +## compress( $ \[, $\] ) + +Accepts a string and compresses it. The second parameter is optional and +can take the value `zlib` or `gzip`, defaulting to `gzip`. + +Returns a compressed string. + +## uncompress( $ ) + +Accepts a string and uncompresses it. The compression method (`gzip` or +`zlib`) is determined automatically. Uncompressed strings are passed +through unaltered. + +Returns an uncompressed string. + +## enchunk( $ \[, $, $\] ) + +Accepts an encoded message (JSON string) and chunks it according to the +GELF chunking protocol. + +The optional second parameter is the maximum size of the chunks to produce, +this must be a positive integer or the special strings `lan` or `wan`, +see ["parse\_size"](#parse_size). Defaults to `wan`. A zero chunk size means no chunking +will be applied. + +The optional third parameter is the message id used to identify associated +chunks. This must be 8 bytes. It defaults to 8 bytes of randomness generated +by [Math::Random::MT](https://metacpan.org/pod/Math::Random::MT). + +If the message size is greater than the maximum size then an array of +chunks is retuned, otherwise the message is retuned unaltered as the first +element of an array. + +## dechunk( \\@, \\% ) + +This facilitates reassembling a GELF message from a stream of chunks. + +It accepts an ARRAYREF for accumulating the chunks and a HASHREF +representing a decoded message chunk as produced by ["decode\_chunk"](#decode_chunk). + +It returns undef if the accumulator is not complete, i.e. all chunks have +not yet been passed it. + +Once the accumulator is complete it returns the de-chunked message in the +form of a string. Note that this message may still be compressed. + +Here is an example usage: + + sub process_chunks { + + my @accumulator; + my $msg; + + do { + $msg = dechunk( + \@accumulator, + decode_chunk(shift()) + ); + } until ($msg); + + return uncompress($msg); + }; + +## is\_chunked( $ ) + +Accepts a string and returns a true value if it is a GELF message chunk. + +## decode\_chunk( $ ) + +Accepts a GELF message chunk and returns an ARRAYREF representing the +unpacked chunk. Dies if the input is not a GELF chunk. + +The message consists of the following keys: + + id + sequence_number + sequence_count + data + +## parse\_level( $ ) + +Accepts a `syslog` style level in the form of a number (1-7) or a string +being one of `emerg`, `alert`, `crit`, `err`, `warn`, `notice`, +`info`, or `debug`. Dies upon invalid input. + +The string forms may also be elongated and will still be accepted. For +example `err` and `error` are equivalent. + +The associated syslog level is returned in numeric form. + +## parse\_size( $ ) + +Accepts an integer specifying the chunk size or the special string values +`lan` or `wan` corresponding to 8154 or 1420 respectively. An explanation +of these values is in the code. + +Returns the passed size or the value corresponding to the `lan` or `wan`. + +["parse\_size"](#parse_size) dies upon invalid input. + +# CONSTANTS + +All Log::Gelf::Util constants are Readonly perl structures. You must use +sigils when referencing them. They can be imported individually and are +included when importing ':all'; + +## $GELF\_MSG\_MAGIC + +The magic number used to identify a GELF message chunk. + +## $ZLIB\_MAGIC + +The magic number used to identify a Zlib deflated message. + +## $GZIP\_MAGIC + +The magic number used to identify a gzipped message. + +## %LEVEL\_NAME\_TO\_NUMBER + +A HASH mapping the level names to numbers. + +## %LEVEL\_NUMBER\_TO\_NAME + +A HASH mapping the level numbers to names. + +## %GELF\_MESSAGE\_FIELDS + +A HASH where each key is a valid core GELF message field name. Deprecated +fields are associated with a false value. + +# LICENSE + +Copyright (C) Strategic Data. + +This library is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +# AUTHOR + +Adam Clarke <adamc@strategicdata.com.au> diff --git a/cpanfile b/cpanfile new file mode 100644 index 0000000..1d603ce --- /dev/null +++ b/cpanfile @@ -0,0 +1,14 @@ +requires 'perl', '5.008001'; + +requires 'Readonly' ,'2.00'; +requires 'Params::Validate'; +requires 'Time::HiRes'; +requires 'Sys::Hostname'; +requires 'JSON::MaybeXS'; +requires 'Math::Random::MT'; + +on 'test' => sub { + requires 'Test::More', '0.98'; + requires 'Test::Exception'; + requires 'Test::Warnings', '0.005'; +}; diff --git a/lib/Log/GELF/Util.pm b/lib/Log/GELF/Util.pm new file mode 100644 index 0000000..1f99c6d --- /dev/null +++ b/lib/Log/GELF/Util.pm @@ -0,0 +1,698 @@ +package Log::GELF::Util; +use 5.010; +use strict; +use warnings; + +require Exporter; +use Readonly; + +our ( + $VERSION, + @ISA, + @EXPORT_OK, + %EXPORT_TAGS, + $GELF_MSG_MAGIC, + $ZLIB_MAGIC, + $GZIP_MAGIC, + %LEVEL_NAME_TO_NUMBER, + %LEVEL_NUMBER_TO_NAME, + %GELF_MESSAGE_FIELDS, + $LEVEL_NAME_REGEX, +); + +$VERSION = "0.96"; + +use Params::Validate qw( + validate + validate_pos + validate_with + SCALAR + ARRAYREF + HASHREF +); +use Time::HiRes qw(time); +use Sys::Syslog qw(:macros); +use Sys::Hostname; +use JSON::MaybeXS qw(encode_json decode_json); +use IO::Compress::Gzip qw(gzip $GzipError); +use IO::Uncompress::Gunzip qw(gunzip $GunzipError); +use IO::Compress::Deflate qw(deflate $DeflateError); +use IO::Uncompress::Inflate qw(inflate $InflateError); +use Math::Random::MT qw(irand); + +Readonly $GELF_MSG_MAGIC => pack('C*', 0x1e, 0x0f); +Readonly $ZLIB_MAGIC => pack('C*', 0x78, 0x9c); +Readonly $GZIP_MAGIC => pack('C*', 0x1f, 0x8b); + +Readonly %LEVEL_NAME_TO_NUMBER => ( + emerg => LOG_EMERG, + alert => LOG_ALERT, + crit => LOG_CRIT, + err => LOG_ERR, + warn => LOG_WARNING, + notice => LOG_NOTICE, + info => LOG_INFO, + debug => LOG_DEBUG, +); + +Readonly %LEVEL_NUMBER_TO_NAME => ( + &LOG_EMERG => 'emerg', + &LOG_ALERT => 'alert', + &LOG_CRIT => 'crit', + &LOG_ERR => 'err', + &LOG_WARNING => 'warn', + &LOG_NOTICE => 'notice', + &LOG_INFO => 'info', + &LOG_DEBUG => 'debug', +); + +Readonly %GELF_MESSAGE_FIELDS => ( + version => 1, + host => 1, + short_message => 1, + full_message => 1, + timestamp => 1, + level => 1, + facility => 0, + line => 0, + file => 0, +); + +my $ln = '^(' . + (join '|', (keys %LEVEL_NAME_TO_NUMBER)) . + ')\w*$'; +$LEVEL_NAME_REGEX = qr/$ln/i; +undef $ln; + +@ISA = qw(Exporter); +@EXPORT_OK = qw( + $GELF_MSG_MAGIC + $ZLIB_MAGIC + $GZIP_MAGIC + %LEVEL_NAME_TO_NUMBER + %LEVEL_NUMBER_TO_NAME + %GELF_MESSAGE_FIELDS + validate_message + encode + decode + compress + uncompress + enchunk + dechunk + is_chunked + decode_chunk + parse_level + parse_size +); + +push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; +Exporter::export_ok_tags('all'); + +sub validate_message { + my %p = validate_with( + params => \@_, + allow_extra => 1, + spec => { + version => { + default => '1.1', + callbacks => { + version_check => sub { + my $version = shift; + $version =~ /^1\.1$/ + or die 'version must be 1.1, supplied $version'; + }, + }, + }, + host => { type => SCALAR, default => hostname() }, + short_message => { type => SCALAR }, + full_message => { type => SCALAR, optional => 1 }, + timestamp => { + type => SCALAR, + default => time(), + callbacks => { + ts_format => sub { + my $ts = shift; + $ts =~ /^\d+(?:\.\d+)*$/ + or die 'bad timestamp'; + }, + }, + }, + level => { type => SCALAR, default => 1 }, + facility => { + type => SCALAR, + optional => 1, + }, + line => { + type => SCALAR, + optional => 1, + callbacks => { + facility_check => sub { + my $line = shift; + $line =~ /^\d+$/ + or die 'line must be a number'; + }, + }, + }, + file => { + type => SCALAR, + optional => 1, + }, + }, + ); + + $p{level} = parse_level($p{level}); + + foreach my $key ( keys %p ) { + + if ( ! $key =~ /^[\w\.\-]+$/ ) { + die "invalid field name '$key'"; + } + + if ( $key eq '_id' || + ! ( exists $GELF_MESSAGE_FIELDS{$key} || $key =~ /^_/ ) + ) { + die "invalid field '$key'"; + } + + if ( exists $GELF_MESSAGE_FIELDS{$key} + && $GELF_MESSAGE_FIELDS{$key} == 0 ) { + # field is deprecated + warn "$key is deprecated, send as additional field instead"; + } + } + + return \%p; +} + +sub encode { + my @p = validate_pos( + @_, + { type => HASHREF }, + ); + + return encode_json(validate_message(@p)); +} + +sub decode { + my @p = validate_pos( + @_, + { type => SCALAR }, + ); + + my $msg = shift @p; + + return validate_message(decode_json($msg)); +} + +sub compress { + my @p = validate_pos( + @_, + { type => SCALAR }, + { + type => SCALAR, + default => 'gzip', + callbacks => { + compress_type => sub { + my $level = shift; + $level =~ /^(?:zlib|gzip)$/ + or die 'compression type must be gzip (default) or zlib'; + }, + }, + }, + ); + + my ($message, $type) = @p; + + my $method = \&gzip; + my $error = \$GzipError; + if ( $type eq 'zlib' ) { + $method = \&deflate; + $error = \$DeflateError; + } + + my $msgz; + &{$method}(\$message => \$msgz) + or die "compress failed: ${$error}"; + + return $msgz; +} + +sub uncompress { + my @p = validate_pos( + @_, + { type => SCALAR } + ); + + my $message = shift @p; + + my $msg_magic = substr $message, 0, 2; + + my $method; + my $error; + if ($ZLIB_MAGIC eq $msg_magic) { + $method = \&inflate; + $error = \$InflateError; + } + elsif ($GZIP_MAGIC eq $msg_magic) { + $method = \&gunzip; + $error = \$GunzipError; + } + else { + #assume plain message + return $message; + } + + my $msg; + &{$method}(\$message => \$msg) + or die "uncompress failed: ${$error}"; + + return $msg; +} + +sub enchunk { + my @p = validate_pos( + @_, + { type => SCALAR }, + { type => SCALAR, default => 'wan' }, + { type => SCALAR, default => pack('L*', irand(),irand()) }, + ); + + my ($message, $size, $message_id) = @p; + + if ( length $message_id != 8 ) { + die "message id must be 8 bytes"; + } + + $size = parse_size($size); + + if ( $size > 0 + && length $message > $size + ) { + my @chunks; + while (length $message) { + push @chunks, substr $message, 0, $size, ''; + } + + my $n_chunks = scalar @chunks; + die 'Message too big' if $n_chunks > 128; + + my $sequence_count = pack('C*', $n_chunks); + + my @chunks_w_header; + my $sequence_number = 0; + foreach my $chunk (@chunks) { + push @chunks_w_header, + $GELF_MSG_MAGIC + . $message_id + . pack('C*',$sequence_number++) + . $sequence_count + . $chunk; + } + + return @chunks_w_header; + } + else { + return ($message); + } +} + +sub dechunk { + my @p = validate_pos( + @_, + { type => ARRAYREF }, + { type => HASHREF }, + ); + + my ($accumulator, $chunk) = @_; + + if ( ! exists $chunk->{id} + && exists $chunk->{sequence_number} + && exists $chunk->{sequence_count} + && exists $chunk->{data} + ) { + die 'malformed chunk'; + } + + if ($chunk->{sequence_number} > $chunk->{sequence_count} ) { + die 'chunk sequence number > count'; + } + + $accumulator->[$chunk->{sequence_number}] = $chunk->{data}; + + if ( (scalar grep {defined} @{$accumulator}) == $chunk->{sequence_count} ) { + return join '', @{$accumulator}; + } + else { + return; + } +} + +sub is_chunked { + my @p = validate_pos( + @_, + { type => SCALAR }, + ); + + my $chunk = shift @p; + + return $GELF_MSG_MAGIC eq substr $chunk, 0, 2; +} + +sub decode_chunk { + my @p = validate_pos( + @_, + { type => SCALAR }, + ); + + my $encoded_chunk = shift; + + if ( is_chunked($encoded_chunk) ) { + + my $id = substr $encoded_chunk, 2, 8; + my $seq_no = unpack('C', substr $encoded_chunk, 10, 1); + my $seq_cnt = unpack('C', substr $encoded_chunk, 11, 1); + my $data = substr $encoded_chunk, 12; + + return { + id => $id, + sequence_number => $seq_no, + sequence_count => $seq_cnt, + data => $data, + }; + } + else { + die "message not chunked"; + } +} + +sub parse_level { + my @p = validate_pos( + @_, + { type => SCALAR } + ); + + my $level = shift @p; + + if ( $level =~ $LEVEL_NAME_REGEX ) { + return $LEVEL_NAME_TO_NUMBER{$1}; + } + elsif ( $level =~ /^(?:0|1|2|3|4|5|6|7)$/ ) { + return $level; + } + else { + die "level must be between 0 and 7 or a valid log level string"; + } +} + +sub parse_size { + my @p = validate_pos( + @_, + { + type => SCALAR, + callbacks => { + compress_type => sub { + my $size = shift; + $size =~ /^(?:lan|wan|\d+)$/i + or die 'chunk size must be "lan", "wan", a positve integer, or 0 (no chunking)'; + }, + }, + }, + ); + + my $size = lc(shift @p); + + # These default values below were determined by + # examining the code for Graylog's implementation. See + # https://github.com/Graylog2/gelf-rb/blob/master/lib/gelf/notifier.rb#L62 + # I believe these are determined by likely MTU defaults + # and possible heasers like so... + # WAN: 1500 - 8 b (UDP header) - 60 b (max IP header) - 12 b (chunking header) = 1420 b + # LAN: 8192 - 8 b (UDP header) - 20 b (min IP header) - 12 b (chunking header) = 8152 b + # Note that based on my calculation the Graylog LAN + # default may be 2 bytes too big (8154) + # See http://stackoverflow.com/questions/14993000/the-most-reliable-and-efficient-udp-packet-size + # For some discussion. I don't think this is an exact science! + + if ( $size eq 'wan' ) { + $size = 1420; + } + elsif ( $size eq 'lan' ) { + $size = 8152; + } + + return $size; +} + +1; +__END__ + +=encoding utf-8 + +=head1 NAME + +Log::GELF::Util - Utility functions for Graylog's GELF format. + +=head1 SYNOPSIS + + use Log::GELF::Util qw( encode ); + + my $msg = encode( { short_message => 'message', } ); + + + use Log::GELF::Util qw( :all ); + + sub process_chunks { + + my @accumulator; + my $msg; + + do { + $msg = dechunk( + \@accumulator, + decode_chunk(shift()) + ); + } until ($msg); + + return uncompress($msg); + }; + + my $hr = validate_message( short_message => 'message' ); + +=head1 DESCRIPTION + +Log::GELF::Util is a collection of functions and data structures useful +when working with Graylog's GELF Format version 1.1. It strives to support +all of the features and options as described in the L. + +=head1 FUNCTIONS + +=head2 validate_message( short_message => $ ) + +Returns a HASHREF representing the validated message with any defaulted +values added to the data structure. + +Takes the following message parameters as per the GELF message +specification: + +=over + +=item short_message + +Mandatory string, a short descriptive message + +=item version + +String, must be '1.1' which is the default. + +=item host + +String, defaults to hostname() from L. + +=item timestamp + +Timestamp, defaults to time() from L. + +=item level + +Integer, equal to the standard syslog levels, default is 1 (ALERT). + +=item facility + +Deprecated, a warning will be issued. + +=item line + +Deprecated, a warning will be issued. + +=item file + +Deprecated, a warning will be issued. + +=item _[additional_field] + +Parameters prefixed with an underscore (_) will be treated as an additional +field. Allowed characters in field names are any word character (letter, +number, underscore), dashes and dots. As per the specification '_id' is +disallowed. + +=back + +=head2 encode( \% ) + +Accepts a HASHREF representing a GELF message. The message will be +validated with L. + +Returns a JSON encoded string representing the message. + +=head2 decode( $ ) + +Accepts a JSON encoded string representing the message. This will be +converted to a hashref and validated with L. + +Returns a HASHREF representing the validated message with any defaulted +values added to the data structure. + +=head2 compress( $ [, $] ) + +Accepts a string and compresses it. The second parameter is optional and +can take the value C or C, defaulting to C. + +Returns a compressed string. + +=head2 uncompress( $ ) + +Accepts a string and uncompresses it. The compression method (C or +C) is determined automatically. Uncompressed strings are passed +through unaltered. + +Returns an uncompressed string. + +=head2 enchunk( $ [, $, $] ) + +Accepts an encoded message (JSON string) and chunks it according to the +GELF chunking protocol. + +The optional second parameter is the maximum size of the chunks to produce, +this must be a positive integer or the special strings C or C, +see L. Defaults to C. A zero chunk size means no chunking +will be applied. + +The optional third parameter is the message id used to identify associated +chunks. This must be 8 bytes. It defaults to 8 bytes of randomness generated +by L. + +If the message size is greater than the maximum size then an array of +chunks is retuned, otherwise the message is retuned unaltered as the first +element of an array. + +=head2 dechunk( \@, \% ) + +This facilitates reassembling a GELF message from a stream of chunks. + +It accepts an ARRAYREF for accumulating the chunks and a HASHREF +representing a decoded message chunk as produced by L. + +It returns undef if the accumulator is not complete, i.e. all chunks have +not yet been passed it. + +Once the accumulator is complete it returns the de-chunked message in the +form of a string. Note that this message may still be compressed. + +Here is an example usage: + + sub process_chunks { + + my @accumulator; + my $msg; + + do { + $msg = dechunk( + \@accumulator, + decode_chunk(shift()) + ); + } until ($msg); + + return uncompress($msg); + }; + +=head2 is_chunked( $ ) + +Accepts a string and returns a true value if it is a GELF message chunk. + +=head2 decode_chunk( $ ) + +Accepts a GELF message chunk and returns an ARRAYREF representing the +unpacked chunk. Dies if the input is not a GELF chunk. + +The message consists of the following keys: + + id + sequence_number + sequence_count + data + +=head2 parse_level( $ ) + +Accepts a C style level in the form of a number (1-7) or a string +being one of C, C, C, C, C, C, +C, or C. Dies upon invalid input. + +The string forms may also be elongated and will still be accepted. For +example C and C are equivalent. + +The associated syslog level is returned in numeric form. + +=head2 parse_size( $ ) + +Accepts an integer specifying the chunk size or the special string values +C or C corresponding to 8154 or 1420 respectively. An explanation +of these values is in the code. + +Returns the passed size or the value corresponding to the C or C. + +L dies upon invalid input. + +=head1 CONSTANTS + +All Log::Gelf::Util constants are Readonly perl structures. You must use +sigils when referencing them. They can be imported individually and are +included when importing ':all'; + +=head2 $GELF_MSG_MAGIC + +The magic number used to identify a GELF message chunk. + +=head2 $ZLIB_MAGIC + +The magic number used to identify a Zlib deflated message. + +=head2 $GZIP_MAGIC + +The magic number used to identify a gzipped message. + +=head2 %LEVEL_NAME_TO_NUMBER + +A HASH mapping the level names to numbers. + +=head2 %LEVEL_NUMBER_TO_NAME + +A HASH mapping the level numbers to names. + +=head2 %GELF_MESSAGE_FIELDS + +A HASH where each key is a valid core GELF message field name. Deprecated +fields are associated with a false value. + +=head1 LICENSE + +Copyright (C) Strategic Data. + +This library is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=head1 AUTHOR + +Adam Clarke Eadamc@strategicdata.com.auE + +=cut diff --git a/minil.toml b/minil.toml new file mode 100644 index 0000000..ac60b8c --- /dev/null +++ b/minil.toml @@ -0,0 +1,4 @@ +name = "Log-GELF-Util" +badges = ["travis"] +module_maker="ModuleBuildTiny" + diff --git a/t/00_compile.t b/t/00_compile.t new file mode 100644 index 0000000..2825bae --- /dev/null +++ b/t/00_compile.t @@ -0,0 +1,9 @@ +use strict; +use Test::More 0.98; + +use_ok $_ for qw( + Log::GELF::Util +); + +done_testing(1); + diff --git a/t/01_utilities.t b/t/01_utilities.t new file mode 100644 index 0000000..59feb47 --- /dev/null +++ b/t/01_utilities.t @@ -0,0 +1,147 @@ +use strict; +use Test::More 0.98; +use Test::Exception; + +use Log::GELF::Util qw(parse_size parse_level); + +throws_ok{ + my %msg = parse_size(); +} +qr/0 parameters were passed.*/, +'parse_size mandatory parameters missing'; + +throws_ok{ + my %msg = parse_size({}); +} +qr/Parameter #1.*/, +'parse_size wrong type'; + +throws_ok{ + my %msg = parse_size(-1); +} +qr/chunk size must be "lan", "wan", a positve integer, or 0 \(no chunking\)/, +'parse_size invalid numeric value'; + +throws_ok{ + my %msg = parse_size('wrong'); +} +qr/chunk size must be "lan", "wan", a positve integer, or 0 \(no chunking\)/, +'parse_size invalid string value'; + +my $size; +lives_ok{ + $size = parse_size(1); +} +'numeric size'; +is($size, 1, 'correct numeric size'); + +lives_ok{ + $size = parse_size('lan'); +} +'string lan size'; +is($size, 8152, 'correct lan size'); + +lives_ok{ + $size = parse_size('LAN'); +} +'string LAN size'; +is($size, 8152, 'correct LAN size'); + +lives_ok{ + $size = parse_size('wan'); +} +'string wan size'; +is($size, 1420, 'correct numeric size'); + +lives_ok{ + $size = parse_size('WAN'); +} +'string WAN size'; +is($size, 1420, 'correct WAN size'); + +throws_ok{ + parse_level(); +} +qr/0 parameters were passed.*/, +'parse_level mandatory parameters missing'; + +throws_ok{ + parse_level({}); +} +qr/Parameter #1.*/, +'parse_level wrong type'; + +throws_ok{ + parse_level(-1); +} +qr/level must be between 0 and 7 or a valid log level string/, +'parse_level invalid numeric value'; + +throws_ok{ + parse_level(8); +} +qr/level must be between 0 and 7 or a valid log level string/, +'parse_level invalid numeric value - too big'; + +throws_ok{ + parse_level('wrong'); +} +qr/level must be between 0 and 7 or a valid log level string/, +'parse_level invalid string value'; + +my $level; +lives_ok{ + $level = parse_level(0); +} +'correct numeric level'; +is($level, 0, 'correct numeric level min'); + +lives_ok{ + $level = parse_level(7); +} +'correct numeric level'; +is($level, 7, 'correct numeric level max'); + +my $level_no = 0; +foreach my $lvl_name ( + qw( + emerg + alert + crit + err + warn + notice + info + debug + ) +) { + lives_ok{ + $level = parse_level($lvl_name); + } + "level $lvl_name ok"; + + is($level, $level_no++, "level $lvl_name correct value"); +} + +$level_no = 0; +foreach my $lvl_name ( + qw( + emergency + alert + critical + error + warning + notice + information + debug + ) +) { + lives_ok{ + $level = parse_level($lvl_name); + } + "level long $lvl_name ok"; + + is($level, $level_no++, "level long $lvl_name correct value"); +} + +done_testing(55); diff --git a/t/02_validate.t b/t/02_validate.t new file mode 100644 index 0000000..1bbc3aa --- /dev/null +++ b/t/02_validate.t @@ -0,0 +1,174 @@ +use strict; +use Test::More 0.98; +use Test::Exception; +use Test::Warnings 0.005 qw(warning allow_warnings); + +use Log::GELF::Util qw(validate_message); + +throws_ok{ + validate_message(); +} +qr/Mandatory parameter 'short_message' missing.*/, +'mandatory parameters missing'; + +throws_ok{ + validate_message( + version => '1.x', + host => 1, + short_message => 1, + ); +} +qr/version must be 1.1, supplied.*/, +'version check'; + +throws_ok{ + validate_message( + host => 1, + short_message => 1, + level => 'x', + ); +} +qr/level must be between 0 and 7 or a valid log level string/, +'level check'; + +throws_ok{ + validate_message( + host => 1, + short_message => 1, + timestamp => 'x', + ); +} +qr/bad timestamp/, +'timestamp check'; + +throws_ok{ + validate_message( + host => 1, + short_message => 1, + bad => 'to the bone.', + ); +} +qr/invalid field 'bad'.*/, +'bad field check'; + +throws_ok{ + validate_message( + host => 1, + short_message => 1, + 'bad name' => 'to the bone.', + ); +} +qr/invalid field 'bad name'.*/, +'bad field check 2'; + +allow_warnings 1; #throws legit warnings +throws_ok{ + validate_message( + host => 1, + short_message => 1, + facility => {}, + ); +} +qr/The 'facility' parameter.*/, +'bad facility check'; + +throws_ok{ + validate_message( + host => 1, + short_message => 1, + line => 'wrong', + ); +} +qr/line must be a number/, +'bad line check'; +allow_warnings 0; + +like( warning { + validate_message( + host => 1, + short_message => 1, + facility => 1, + ); +}, +qr/^facility is deprecated.*/, +'facility deprecated'); + +like( warning { + validate_message( + host => 1, + short_message => 1, + line => 1, + ); +}, +qr/^line is deprecated.*/, +'line deprecated'); + +like( warning { + validate_message( + host => 1, + short_message => 1, + file => 1, + ); +}, +qr/^file is deprecated.*/, +'file deprecated'); + +my $msg; +lives_ok{ + $msg = validate_message( + host => 1, + short_message => 1, + ); +} +'default version'; + +my $time = time; +is($msg->{version}, '1.1', 'correct default version'); +like($msg->{timestamp}, qr/\d+\.\d+/, 'default timestamp'); +is($msg->{level}, 1, 'default level'); + +lives_ok{ + $msg = validate_message( + host => 1, + short_message => 1, + level => 2, + ); +} +'numeric level'; +is($msg->{level}, 2, 'default level'); + +lives_ok{ + $msg = validate_message( + host => 1, + short_message => 1, + level => 'err', + ); +} +'numeric level'; +is($msg->{level}, 3, 'default level'); + +allow_warnings 1; #throws legit warnings +lives_ok{ + $msg = validate_message( + host => 1, + short_message => 1, + facility => 1, + ); +} +'facility check'; +ok(exists $msg->{facility}, 'line exists'); +is($msg->{facility}, '1', 'correct facility'); + +lives_ok{ + $msg = validate_message( + host => 1, + short_message => 1, + line => 1, + ); +} +'line check'; +ok(exists $msg->{line}, 'line exists'); +is($msg->{line}, '1', 'correct line'); +allow_warnings 0; + +done_testing(26); diff --git a/t/03_encode.t b/t/03_encode.t new file mode 100644 index 0000000..74c803a --- /dev/null +++ b/t/03_encode.t @@ -0,0 +1,60 @@ +use strict; +use Test::More 0.98; +use Test::Exception; +use JSON::MaybeXS qw(decode_json); + +use Log::GELF::Util qw(encode decode); + +throws_ok{ + my %msg = encode(); +} +qr/0 parameters were passed.*/, +'mandatory encode parameter missing'; + +throws_ok{ + my %msg = encode({}); +} +qr/Mandatory parameter 'short_message' missing.*/, +'mandatory encode parameters missing'; + +my $msg; +lives_ok{ + $msg = decode_json(encode( + { + host => 'host', + short_message => 'message', + } + )); +} +'encodes ok'; + +is($msg->{version}, '1.1', 'correct default version'); +is($msg->{host}, 'host', 'correct default version'); + +throws_ok{ + my %msg = decode(); +} +qr/0 parameters were passed.*/, +'mandatory decode parameter missing'; + +throws_ok{ + my %msg = decode("{}"); +} +qr/Mandatory parameter 'short_message' missing.*/, +'mandatory encode parameters missing'; + +lives_ok{ + $msg = decode(encode( + { + host => 'host', + short_message => 'message', + } + )); +} +'encodes ok'; + +is($msg->{version}, '1.1', 'correct default version'); +is($msg->{host}, 'host', 'correct default version'); + +done_testing(10); + diff --git a/t/04_compress.t b/t/04_compress.t new file mode 100644 index 0000000..9b4db4e --- /dev/null +++ b/t/04_compress.t @@ -0,0 +1,116 @@ +use strict; +use Test::More 0.91; +use Test::Exception; +use IO::Uncompress::Gunzip qw(gunzip $GunzipError); +use IO::Uncompress::Inflate qw(inflate $InflateError) ; +use JSON::MaybeXS qw(decode_json); + +use Log::GELF::Util qw(encode compress uncompress); + +throws_ok{ + my %msg = compress(); +} +qr/0 parameters were passed.*/, +'mandatory parameter missing'; + +throws_ok{ + compress({}); +} +qr/Parameter #1.*/, +'message parameters wrong type'; + +throws_ok{ + my %msg = compress(1,'wrong'); +} +qr/compression type must be gzip \(default\) or zlib/, +'type parameters wrong'; + +throws_ok{ + my %msg = uncompress(); +} +qr/0 parameters were passed.*/, +'mandatory parameter missing'; + +throws_ok{ + my %msg = uncompress( + {}, + ); +} +qr/Parameter #1.*/, +'message parameters wrong type'; + +lives_ok{ + compress( 1, 'gzip'); +} +'gzips explicit ok'; + +lives_ok{ + compress( 1, 'zlib'); +} +'zlib explicit ok'; + +my $msgz; +lives_ok{ + $msgz = compress( + encode( + { + host => 'host', + short_message => 'message', + } + ) + ); +} +'gzips ok'; + +my $msgj; +gunzip \$msgz => \$msgj + or die "gunzip failed: $GunzipError"; +my $msg = decode_json($msgj); + +is($msg->{version}, '1.1', 'correct default version'); +is($msg->{host}, 'host', 'correct default version'); + +lives_ok{ + $msg = decode_json( + uncompress($msgz) + ); +} +'uncompresses gzip ok'; + +is($msg->{version}, '1.1', 'correct default version'); +is($msg->{host}, 'host', 'correct default version'); + +my $msgz; +lives_ok{ + $msgz = compress( + encode( + { + host => 'host', + short_message => 'message', + } + ), + 'zlib', + ); +} +'deflates ok'; + +my $msgj; +inflate \$msgz => \$msgj + or die "inflate failed: $InflateError"; +my $msg = decode_json($msgj); + +is($msg->{version}, '1.1', 'correct default version'); +is($msg->{host}, 'host', 'correct default version'); + +lives_ok{ + $msg = decode_json( + uncompress($msgz) + ); +} +'uncompresses zlib ok'; + +is($msg->{version}, '1.1', 'correct default version'); +is($msg->{host}, 'host', 'correct default version'); + +done_testing(19); + diff --git a/t/05_chunked.t b/t/05_chunked.t new file mode 100644 index 0000000..10ffe28 --- /dev/null +++ b/t/05_chunked.t @@ -0,0 +1,186 @@ +use strict; +use warnings; + +use Test::More; +use Test::Exception; +use List::Util qw(shuffle); +use Math::Random::MT qw(irand); + +use Log::GELF::Util qw( + decode_chunk + compress + uncompress + is_chunked + enchunk + dechunk + decode_chunk + encode + $GELF_MSG_MAGIC +); + +use JSON::MaybeXS qw(decode_json); +use IO::Uncompress::Gunzip qw(gunzip $GunzipError); +use IO::Uncompress::Inflate qw(inflate $InflateError); + +sub test_dechunk { + + my @chunks; + my $msg; + + do { + $msg = dechunk(\@chunks, decode_chunk(shift())); + } until ($msg); + + return uncompress( $msg ); +}; + +throws_ok{ + is_chunked(); +} +qr/0 parameters were passed.*/, +'mandatory parameters missing'; + +ok( ! is_chunked( 'no magic' ), 'no magic' ); + +ok( is_chunked( $GELF_MSG_MAGIC ), 'magic' ); + +throws_ok{ + enchunk(); +} +qr/0 parameters were passed to Log::GELF::Util::enchunk but 3 were expected/, +'mandatory parameters missing'; + +throws_ok{ + enchunk('0123456789', -1); +} +qr/chunk size must be "lan", "wan", a positve integer, or 0 \(no chunking\)/, +'enchunk negative size'; + +throws_ok{ + enchunk('0123456789', 'xxx'); +} +qr/chunk size must be "lan", "wan", a positve integer, or 0 \(no chunking\)/, +'enchunk bad size'; + +my @chunks; +lives_ok{ + @chunks = enchunk('0123456789'); +} +'enchunks ok - size default'; + +lives_ok{ + @chunks = enchunk('0123456789', 0); +} +'enchunks ok - 0'; +is(scalar @chunks, 1, 'correct number of chunks - 0'); + +lives_ok{ + @chunks = enchunk('0123456789', 1); +} +'enchunks ok - 1'; +is(scalar @chunks, 10, 'correct number of chunks - 1'); + +lives_ok{ + @chunks = enchunk( + encode( + { + host => 'host', + short_message => 'message', + } + ), + 4 + ); +} +'enchunks ok - message'; + +throws_ok{ + decode_chunk(); +} +qr/0 parameters were passed.*/, +'mandatory parameter to decode_chunk missing'; + +my $chunk; +lives_ok{ + $chunk = decode_chunk($chunks[0]); +} +'decode chunk succeeds'; + +ok($chunk->{id}, 'id exists'); +is($chunk->{sequence_number}, 0, 'sequence correct'); +is($chunk->{sequence_count}, scalar @chunks, 'sequence correct'); +is(length($chunk->{data}), 4, 'chunk size correct'); + +my $msg = decode_json(test_dechunk(@chunks)); +is($msg->{version}, '1.1', 'correct default version'); +is($msg->{host}, 'host', 'correct default version'); + +lives_ok{ + @chunks = enchunk( + compress( + encode( + { + host => 'host', + short_message => 'message', + } + ) + ), + 4 + ); +} +'enchunks compressed gzip ok'; + +foreach my $i (1 .. 10) { + + $msg = decode_json(test_dechunk(shuffle @chunks)); + is($msg->{version}, '1.1', "correct default version $i"); + is($msg->{host}, 'host', "correct host $i"); + +} + +lives_ok{ + @chunks = enchunk( + compress( + encode( + { + host => 'host', + short_message => 'message', + } + ), + 'zlib', + ), + 4 + ); +} +'enchunks compressed zlib ok'; + +$msg = decode_json(test_dechunk(@chunks)); +is($msg->{version}, '1.1', 'correct default version'); +is($msg->{host}, 'host', 'correct host'); + +throws_ok{ + enchunk('0123456789', 2, '1234'); +} +qr/message id must be 8 bytes/, +'message id bad size'; + +my @message_id = ( 1498382863, 3314914434 ); +my $message_id = pack('L*', @message_id ); +lives_ok{ + @chunks = enchunk('0123456789', 2, $message_id ); +} +'enchunks with message id'; + +my %ids; +foreach my $chunk (@chunks) { + my $decoded_chunk = decode_chunk($chunk); + $ids{$decoded_chunk->{id}} = 1; +} + +is(scalar keys %ids, 1, 'one id across chunks'); + +$msg = decode_chunk(shift @chunks); +is($msg->{id}, $message_id, 'correct packed message id'); +is((join '', unpack('LL', $msg->{id})), (join '', @message_id), 'correct message id'); + +done_testing(49); +